我正在尝试在R中执行一个代码,它应该给我两件事
答案 0 :(得分:4)
apply(data, 1, function(x) ifelse(sum(is.na(x)) == 3, "", tail(na.omit(x), 1)))
答案 1 :(得分:2)
以下解决方案使用is.na
检查是否为NA,cumsum
创建了到目前为止的NA的累计数量,which.max
来查找最后的非NA NA值:
apply(pr_data, 1,
function(row_data) row_data[which.max(cumsum(!is.na(row_data)))])
例如在一行上(使代码看起来不像黑魔法......):
row_data = pr_data[5,]
# pr1 pr2 pr3
# 5 <NA> p3 <NA>
!is.na(row_data)
# pr1 pr2 pr3
# 5 FALSE TRUE FALSE
cumsum(!is.na(row_data))
# [1] 0 1 1
which.max(cumsum(!is.na(row_data)))
# Returns the first occurence of the max value, which is the last non-NA value
row_data[which.max(cumsum(!is.na(row_data)))]
# The actual value
# pr2
# 5 p3
答案 2 :(得分:1)
以下是使用max.col
选择最终位置的另一个基本R方法rowSums
和==
,以检查行中的所有元素是否为NA,最后是矩阵子集。< / p>
# construct logical matrix of NA positions since it is (sort of) used twice
naMat <- is.na(pr_data)
# locate final non-NA position for each row
myOut <- max.col(!naMat, ties.method = "last")
# set NA to positions where all elements in row are NA
is.na(myOut) <- rowSums(naMat) == ncol(pr_data)
# use matrix to select desired elements from original data.frame
pr_data$out <- pr_data[cbind(seq_len(nrow(pr_data)), myOut)]
返回
pr_data
pr1 pr2 pr3 out
1 <NA> <NA> <NA> <NA>
2 p1 p2 <NA> p2
3 p1 p2 p3 p3
4 <NA> p2 p4 p4
5 <NA> p3 <NA> p3
6 p2 <NA> <NA> p2
答案 3 :(得分:0)
链式ifelse
是一种方法......
# Creating data set
pr1 <- as.character(c(NA, 'p1', 'p1', NA, NA, 'p2'))
pr2 <- as.character(c(NA, 'p2', 'p2', 'p2', 'p3', NA))
pr3 <- as.character(c(NA, NA, 'p3', 'p4', NA, NA))
pr_data <- data.frame(pr1, pr2, pr3)
pr_data <- data.frame(lapply(pr_data, as.character), stringsAsFactors=FALSE)
str(pr_data)
# Actual solution
pr_data$required_output <- ifelse(!is.na(pr_data$pr3),
pr_data$pr3,
ifelse(!is.na(pr_data$pr2),
pr_data$pr2,
pr_data$pr1))