我有df
,如下所示
df <- structure(list(V1 = structure(c(3L,),
class = "factor")),
.Names = c("V1", "V2", "V3"), class = "data.frame",
row.names = c(NA, -7L))
我希望在第一个|
所以输出看起来像这样
out<- structure(list(V1 = structure(c(3L, 2L, 4L, 1L, 6L, 5L, 7L), .Label = c("O15111",
"P41250", "P62805", "Q13547", "Q16555", "Q8NBS9", "Q9H6T3"), class = "factor"), ta.frame", row.names = c(NA, -7L))
答案 0 :(得分:2)
我们可以遍历df
中的每一列,并在第一个|
后用空格替换所有内容
df[] <- lapply(df, function(x) sub("\\|.*", "", as.character(x)))
df
# V1 V2 V3
#1 P62805 Q71DI3
#2 P41250 P12081 P34896
#3 Q13547 P62805
#4 O15111 Q13748
#5 Q8NBS9 Q12792
#6 Q16555 Q14195-2 P21359
#7 Q9H6T3 Q9Y230 Q9Y265
修改强>
根据评论中的更新,将列粘贴在一起并删除空的值
apply(df, 1, function(x) paste0(sub("\\|.*", "", as.character(x[x!=""])), collapse = ","))
#[1] "P62805,Q71DI3" "P41250,P12081,P34896" "Q13547,P62805" "O15111,Q13748"
#[5] "Q8NBS9,Q12792" "Q16555,Q14195-2,P21359" "Q9H6T3,Q9Y230,Q9Y265"