我有类似的数据:
DT <- data.table(name=c("a","b","c","total"),measure1=c(1,1,1,3),
measure2=c(1,1,2,4),measure3=c(2,2,2,6),measure4=c(3,3,3,9))
name measure1 measure2 measure3 measure4
1: a 1 1 2 3
2: b 1 1 2 3
3: c 1 2 2 3
4: total 3 4 6 9
我想选择name
列以及最后一行(在name
列中具有“总计”)大于5的所有列。所需的输出是这样的:
name measure3 measure4
1: a 2 3
2: b 2 3
3: c 2 3
4: total 6 9
感谢您的帮助。
答案 0 :(得分:1)
一种收集-传播-聚集tidyverse
方法:
library(dplyr)
library(tidyr)
DT %>%
gather(key,val,-name) %>%
spread(name,val) %>%
filter(total>5) %>%
gather(name,val,-key) %>%
spread(key,val)
name measure3 measure4
1 a 2 3
2 b 2 3
3 c 2 3
4 total 6 9
答案 1 :(得分:0)
感谢@akrun和@Jaap在上面的评论中使用data.table发布他们的答案。两者都可以完美地工作,并且是很好的解决方案,两者之间在风格上有所不同。为了完整起见,以下是两个答案:
DT[, DT[.N][, c(TRUE,.SD > 5), .SDcols = -1], with = FALSE]
和
DT[, .SD, .SDcols = DT[name == "total", c(TRUE, .SD > 5), .SDcols = -1]]