我有一个看起来像这样的表:
step1 | step2 | step3 | step4
A A B C
A A NA NA
我想要实现的是
step1 | step2 | step3 | step4
A | B | C | NA
A | NA | NA | NA
我先尝试转置然后再运行
table4 <- table3[match(unique(table3[,1]), table3[,1]), ]
但这会将表格切断为匹配的唯一字符串数量。
非常感谢任何帮助。 谢谢。
答案 0 :(得分:1)
一个选项是循环行,replace
duplicated
元素到NA
,然后order
循环缺失值,转置输出并将其分配给原始数据
table3[] <- t(apply(table3, 1, function(x) {
x1 <- replace(x, duplicated(x), NA)
x1[order(is.na(x1))]}))
table3
# step1 step2 step3 step4
#1 A B C <NA>
#2 A <NA> <NA> <NA>
table3 <- structure(list(step1 = c("A", "A"), step2 = c("A", "A"),
step3 = c("B", NA), step4 = c("C", NA)), .Names = c("step1",
"step2", "step3", "step4"), class = "data.frame", row.names = c(NA, -2L))