如果一列中的元素不是NA
,我希望paste0
兩列。如果一列中的一個元素
是NA
,那麼只保留另一列的元素。R paste0 2列如果不是NA
structure(list(col1 = structure(1:3, .Label = c("A", "B", "C"),
class = "factor"), col2 = c(1, NA, 3)), .Names = c("col1", "col2"),
class = "data.frame",row.names = c(NA, -3L))
# col1 col2
# 1 A 1
# 2 B NA
# 3 C 3
structure(list(col1 = structure(1:3, .Label = c("A", "B", "C"),
class = "factor"),col2 = c(1, NA, 3), col3 = c("A|1", "B", "C|3")),
.Names = c("col1", "col2", "col3"), row.names = c(NA,-3L),
class = "data.frame")
# col1 col2 col3
#1 A 1 A|1
#2 B NA B
#3 C 3 C|3
你在哪裏卡住了?這應該很容易使用'ifelse'。 – Roland 2014-09-10 14:05:58
你說你想使用'paste0',但你的例子看起來像'paste'的結果... – 2014-09-10 14:13:19
@BenBolker這是我的錯字。我的意思是過去0。將糾正它。有沒有辦法執行這個任務沒有ifelse? – DJJ 2014-09-10 14:15:15