我想在R中的每一行中选择最后2个值(忽略NA)并将最后2个值放在新的data.frame
中这是一个可重复的例子
V10 V11 V12 V13 V14 V15
1 116 174 NA NA NA NA
2 NA NA NA NA NA NA
3 NA NA NA NA NA NA
4 NA NA NA NA NA NA
5 NA NA NA NA NA NA
6 NA NA NA NA NA NA
7 NA NA NA NA NA NA
957594 115 116 120 613 127 127
957595 NA NA NA NA NA NA
957596 NA NA NA NA NA NA
tss2 <- structure(list(V10 = c(116L, NA, NA, NA, NA, NA, NA, 115L, NA,NA),
V11 = c(174L, NA, NA, NA, NA, NA, NA, 116L, NA, NA),
V12 = c(NA,NA, NA, NA, NA, NA, NA, 120L, NA, NA),
V13 = c(NA, NA, NA, NA,NA, NA, NA, 613L, NA, NA),
V14 = c(NA, NA, NA, NA, NA, NA, NA,127L, NA, NA),
V15 = c(NA, NA, NA, NA, NA, NA, NA, 127L,
NA,NA)), .Names = c("V10", "V11", "V12", "V13",
"V14", "V15"), class = "data.frame", row.names
= c(1L,2L, 3L, 4L, 5L, 6L, 7L, 957594L, 957595L, 957596L))
预期结果为
Pos1 | Pos2
116 | 174
127 | 127
答案 0 :(得分:4)
转换为矩阵可能会加快速度:
ma = apply(as.matrix(tss2), MARGIN = 1, FUN = function(x) tail(na.omit(x), 2))
# risky version: should work *if* there aren't rows with exactly one non-missing element
matrix(unlist((ma)), ncol = 2, byrow = T)
# [,1] [,2]
# [1,] 116 174
# [2,] 127 127
# safe version
matrix(unlist((ma[sapply(ma, function(x) length(x) == 2)])), ncol = 2, byrow = T)
答案 1 :(得分:1)
您可以使用以下循环:
new_data <- list()
for(i in 1:nrow(tss2)) {
new_data[[i]] <- tail(as.numeric(tss2[i, ][complete.cases(as.numeric(tss2[i, ]))]), 2)
}
new_data <- new_data[lapply(new_data, length) == 2]
new_data <- t(matrix(unlist(new_data), ncol = 2))