Return 不是 NA 的最后一个数据框列
Return last data frame column which is not NA
我有一个数据集,其中包含多个标记为 1 或 NA 的案例。我正在尝试找出一种方法来 return 每种情况下都不是 NA 的最高编号邮票。
以下是一些示例数据:
PIN <- c("case1", "case2", "case3", "case4", "case5")
STAMP_1 <- c(1, 1, 1, 1, 1)
STAMP_2 <- c(NA, 1, 1, NA, 1)
STAMP_3 <- c(1, NA, 1, 1, NA)
STAMP_4 <- c(NA, NA, 1, 1, NA)
STAMP_5 <- c(1, NA, NA, 1, NA)
data <- data.frame(PIN, STAMP_1, STAMP_2, STAMP_3, STAMP_4, STAMP_5)
我想找到一种方法来 return 一个包含列的数据框:"case1"、"case2"、"case3"、"case4"、"case5" 和 "STAMP_5"、"STAMP_2"、"STAMP_4"、"STAMP_5"、"STAMP_2" 在这种情况下。
这里是 max.col
、is.na
和 names
的方法。 max.col
查找每行具有最大值的列。在这里,我们为它提供 is.na
的值,它是 TRUE 或 FALSE,并使用 ties.method="last" 来获取最终的非 NA 值。这个位置用来索引names(dat)
.
data.frame(PIN=dat$PIN,
stamp=names(dat)[-1][max.col(!is.na(dat[-1]), ties.method="last")])
PIN stamp
1 case1 STAMP_5
2 case2 STAMP_2
3 case3 STAMP_4
4 case4 STAMP_5
5 case5 STAMP_2
如果您有一整行都带有 NA,max.col
将 return 该行的最终位置(静默失败?)。 return 一个 NA 而不是那个位置的一种方法是使用 NA 和求幂的技巧。在这里,我们 apply
遍历行并找到具有至少一个非 NA 值 return FALSE(或 0)的 any
行的任何 NA 行。
data.frame(PIN=dat$PIN,
stamp=names(dat)[-1][
max.col(!is.na(dat[-1]), ties.method="last") * NA^!rowSums(!is.na(dat[-1]))])
在 Frank 的建议下,我从 applyapply(dat[-1], 1, function(x) all(is.na(x)))
切换到 !rowSums(!is.na(dat[-1]))
。这应该比 apply
.
快很多
通过使用 dplyr
和 melt
(来自 reshape
)
dat=melt(dat)
dat=na.omit(dat)
dat%>%group_by(PIN)%>%slice(n())
# A tibble: 5 x 3
# Groups: PIN [5]
PIN variable value
<fctr> <fctr> <dbl>
1 case1 STAMP_5 1
2 case2 STAMP_2 1
3 case3 STAMP_4 1
4 case4 STAMP_5 1
5 case5 STAMP_2 1
基础 R
temp = cbind(NA, data[-1])
temp = temp * col(temp)
data.frame(PIN = data$PIN,
STAMP = names(temp)[max.col(m = replace(temp, is.na(temp), 0),
ties.method = "first")])
# PIN STAMP
#1 case1 STAMP_5
#2 case2 STAMP_2
#3 case3 STAMP_4
#4 case4 STAMP_5
#5 case5 STAMP_2
我有一个数据集,其中包含多个标记为 1 或 NA 的案例。我正在尝试找出一种方法来 return 每种情况下都不是 NA 的最高编号邮票。
以下是一些示例数据:
PIN <- c("case1", "case2", "case3", "case4", "case5")
STAMP_1 <- c(1, 1, 1, 1, 1)
STAMP_2 <- c(NA, 1, 1, NA, 1)
STAMP_3 <- c(1, NA, 1, 1, NA)
STAMP_4 <- c(NA, NA, 1, 1, NA)
STAMP_5 <- c(1, NA, NA, 1, NA)
data <- data.frame(PIN, STAMP_1, STAMP_2, STAMP_3, STAMP_4, STAMP_5)
我想找到一种方法来 return 一个包含列的数据框:"case1"、"case2"、"case3"、"case4"、"case5" 和 "STAMP_5"、"STAMP_2"、"STAMP_4"、"STAMP_5"、"STAMP_2" 在这种情况下。
这里是 max.col
、is.na
和 names
的方法。 max.col
查找每行具有最大值的列。在这里,我们为它提供 is.na
的值,它是 TRUE 或 FALSE,并使用 ties.method="last" 来获取最终的非 NA 值。这个位置用来索引names(dat)
.
data.frame(PIN=dat$PIN,
stamp=names(dat)[-1][max.col(!is.na(dat[-1]), ties.method="last")])
PIN stamp
1 case1 STAMP_5
2 case2 STAMP_2
3 case3 STAMP_4
4 case4 STAMP_5
5 case5 STAMP_2
如果您有一整行都带有 NA,max.col
将 return 该行的最终位置(静默失败?)。 return 一个 NA 而不是那个位置的一种方法是使用 NA 和求幂的技巧。在这里,我们 apply
遍历行并找到具有至少一个非 NA 值 return FALSE(或 0)的 any
行的任何 NA 行。
data.frame(PIN=dat$PIN,
stamp=names(dat)[-1][
max.col(!is.na(dat[-1]), ties.method="last") * NA^!rowSums(!is.na(dat[-1]))])
在 Frank 的建议下,我从 applyapply(dat[-1], 1, function(x) all(is.na(x)))
切换到 !rowSums(!is.na(dat[-1]))
。这应该比 apply
.
通过使用 dplyr
和 melt
(来自 reshape
)
dat=melt(dat)
dat=na.omit(dat)
dat%>%group_by(PIN)%>%slice(n())
# A tibble: 5 x 3
# Groups: PIN [5]
PIN variable value
<fctr> <fctr> <dbl>
1 case1 STAMP_5 1
2 case2 STAMP_2 1
3 case3 STAMP_4 1
4 case4 STAMP_5 1
5 case5 STAMP_2 1
基础 R
temp = cbind(NA, data[-1])
temp = temp * col(temp)
data.frame(PIN = data$PIN,
STAMP = names(temp)[max.col(m = replace(temp, is.na(temp), 0),
ties.method = "first")])
# PIN STAMP
#1 case1 STAMP_5
#2 case2 STAMP_2
#3 case3 STAMP_4
#4 case4 STAMP_5
#5 case5 STAMP_2