合并多列,排除空值 [英] Combine multiple columns, excluding null values
问题描述
我正在尝试找出如何组合多列(不包括NA值)。
I'm trying to figure out how to combine multiple columns, excluding NA values.
输入数据框:
data <- data.frame(
id = c(1:3),
Item1 = c("Egg", "", ""),
Item2 = c("Chicken", "Flour", ""),
Item3 = c("", "", "Bread"),
Item4 = c("", "Milk", "")
)
所需数据框:
desired <- data.frame(
id = c(1:3),
Item1 = c("Egg", "", ""),
Item2 = c("Chicken", "Flour", ""),
Item3 = c("", "", "Bread"),
Item4 = c("", "Milk", ""),
Combine = c("Egg, Chicken", "Flour, Milk", "Bread")
)
我尝试使用以下代码组合值:
I have tried combining the values using the following code:
data$Combine = paste(data$Item1, data$Item2, data$Item3, data$Item4, sep=",")
问题是我得到这样的结果:
The issue is that I'm getting results like this:
蛋icken ,,
,面粉,,牛奶
,,面包
Egg,Chicken,,
,Flour,,Milk
,,Bread,
推荐答案
如果我们使用与OP中类似的方法,请用
替换前导/滞后空格,并重复不止一个空格,
和单个,
使用 gsub
If we use the similar approach as in the OP's post, replace the leading/lagging spaces with ""
and those having more than one repeating ,
with a single ,
using gsub
data$Combine <- gsub(",{2,}", ",",
gsub("^,+|,+$", "", do.call(paste, c(data[-1], sep=","))))
data$Combine
#[1] "Egg,Chicken" "Flour,Milk" "Bread"
或者另一种选择是使用粘贴
,删除前导/滞后空格(修剪
),然后替换一个或多个空格( \\s +
)和,
使用 gsub
Or another option is to use paste
, remove the leading/lagging spaces (trimws
) and then replace one or more spaces (\\s+
) with a ,
using gsub
gsub("\\s+", ",", trimws(do.call(paste, data[-1])))
#[1] "Egg,Chicken" "Flour,Milk" "Bread"
data
data
data <- structure(list(ID = 1:3, Item1 = c("Egg", "", ""), Item2 = c("Chicken",
"Flour", ""), Item3 = c("", "", "Bread"), Item4 = c("", "Milk",
"")), .Names = c("ID", "Item1", "Item2", "Item3", "Item4"),
class = "data.frame", row.names = c(NA, -3L))
这篇关于合并多列,排除空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!