连接避免 NA 以产生多行变量
Concatenate avoiding NAs to produce multiline variable
虚假数据
Fruit <- c("Tomato", "Banana", "Kiwi", "Pear")
Colour <- c("Red", "Yellow", NA, "Green")
Weight <- c(10, 12, 6, 8)
dd <- data.frame(Fruit, Colour, Weight)
尝试失败
dd <- dd %>%
mutate(Description = sprintf("%s: %s \n%s: %s \n%s: %s",
names(dd)[1], Fruit,
names(dd)[2], Colour,
names(dd)[3], Weight))
dd$Description[1]
所需输出:多行 "Description"变量忽略 NA。
"Description" 番茄变量:
Fruit: Tomato
Colour: Red
Weight: 10
"Description" Kiwi 变量,NA 忽略!
Fruit: Kiwi
Weight: 6
这感觉有点老套,但对于基本的 R 解决方案,我们可以使用 ifelse
和 is.na
有条件地呈现一个属性,或者只是空字符串:
sprintf("%s\n%s\n%s",
ifelse(is.na(Fruit), "", paste0(names(dd)[1], ": ", Fruit)),
ifelse(is.na(Colour), "", paste0(names(dd)[2], ": ", Colour)),
ifelse(is.na(Weight), "", paste0(names(dd)[3], ": ", Weight)))
[1] "Fruit: Tomato\nColour: Red\nWeight: 10"
[2] "Fruit: Banana\nColour: Yellow\nWeight: 12"
[3] "Fruit: Kiwi\n\nWeight: 6" <-- Kiwi has no colour
[4] "Fruit: Pear\nColour: Green\nWeight: 8"
遍历行,删除 NA,然后粘贴:
dd$Description <- unlist(
apply(dd, 1, function(i) {
x <- na.omit(i)
paste(paste0(names(x),":", x), collapse = "\n")
}))
dd
# Fruit Colour Weight Description
# 1 Tomato Red 10 Fruit:Tomato\nColour:Red\nWeight:10
# 2 Banana Yellow 12 Fruit:Banana\nColour:Yellow\nWeight:12
# 3 Kiwi <NA> 6 Fruit:Kiwi\nWeight: 6
# 4 Pear Green 8 Fruit:Pear\nColour:Green\nWeight: 8
虚假数据
Fruit <- c("Tomato", "Banana", "Kiwi", "Pear")
Colour <- c("Red", "Yellow", NA, "Green")
Weight <- c(10, 12, 6, 8)
dd <- data.frame(Fruit, Colour, Weight)
尝试失败
dd <- dd %>%
mutate(Description = sprintf("%s: %s \n%s: %s \n%s: %s",
names(dd)[1], Fruit,
names(dd)[2], Colour,
names(dd)[3], Weight))
dd$Description[1]
所需输出:多行 "Description"变量忽略 NA。
"Description" 番茄变量:
Fruit: Tomato
Colour: Red
Weight: 10
"Description" Kiwi 变量,NA 忽略!
Fruit: Kiwi
Weight: 6
这感觉有点老套,但对于基本的 R 解决方案,我们可以使用 ifelse
和 is.na
有条件地呈现一个属性,或者只是空字符串:
sprintf("%s\n%s\n%s",
ifelse(is.na(Fruit), "", paste0(names(dd)[1], ": ", Fruit)),
ifelse(is.na(Colour), "", paste0(names(dd)[2], ": ", Colour)),
ifelse(is.na(Weight), "", paste0(names(dd)[3], ": ", Weight)))
[1] "Fruit: Tomato\nColour: Red\nWeight: 10"
[2] "Fruit: Banana\nColour: Yellow\nWeight: 12"
[3] "Fruit: Kiwi\n\nWeight: 6" <-- Kiwi has no colour
[4] "Fruit: Pear\nColour: Green\nWeight: 8"
遍历行,删除 NA,然后粘贴:
dd$Description <- unlist(
apply(dd, 1, function(i) {
x <- na.omit(i)
paste(paste0(names(x),":", x), collapse = "\n")
}))
dd
# Fruit Colour Weight Description
# 1 Tomato Red 10 Fruit:Tomato\nColour:Red\nWeight:10
# 2 Banana Yellow 12 Fruit:Banana\nColour:Yellow\nWeight:12
# 3 Kiwi <NA> 6 Fruit:Kiwi\nWeight: 6
# 4 Pear Green 8 Fruit:Pear\nColour:Green\nWeight: 8