0

我觉得这是一个非常愚蠢的问题,但我也无法找到解决方案

我有一个小标题,其中每一行都是一个样本,第一列是一个包含样本 ID 的字符变量,所有后续列都是带有数字变量的变量。

例如:

id <- c("a", "b", "c", "d", "e")
x1 <- rep(1,5)
x2 <- seq(1,5,1)
x3 <- rep(2,5)    
x4 <- seq(0.1, 0.5, 0.1)
tb <- tibble(id, x1, x2, x3, x4) 

我想对此进行子集化以仅包括总和大于 5 的列和 id 列。使用旧的数据框结构,我知道以下工作:

df <- as.data.frame(tb)
df2 <- cbind(df$id, df[,colSums(df[,2:5])>5)
colnames(df2)[1] <- "id"

但是,当我尝试使用 tibble 以这种方式进行子集化时,我收到错误消息:

Error: Length of logical index vector must be 1 or 5, got: 4

有谁知道如何在不转换为旧数据框格式的情况下完成这项任务?最好不要创建缺少 id 变量的中间 tibble,因为将我的 id 从我的数据中分离出来只是在寻找麻烦。

谢谢!

4

1 回答 1

0
# install.packages(c("tidyverse"), dependencies = TRUE)
library(tibble)
df <- tibble(id = letters[1:5], x1 = 1, x2 = 1:5, x3 = 2, x4 = seq(.1, .5, len = 5))
### two additional examples of how to generate the Tibble data
### exploiting that its arguments are evaluated lazily and sequentially
# df <- tibble(id = letters[1:5], x1 = 1, x2 = 1:5, x3 = x1 + 1, x4 = x2/10)
# df <- tibble(x2 = 1:5, id = letters[x2], x3 = 2, x1 = x3-1, x4 = x2/10) %>%
#              select(id, num_range("x", 1:4))

base R解决方案,参见 HubertL 上面的评论

###  HubertL's base solution
df[c(TRUE,colSums(df[2:5])>5)]
#> # A tibble: 5 x 3
#>      id    x2    x3
#>   <chr> <int> <dbl>
#> 1     a     1     2
#> 2     b     2     2
#> 3     c     3     2
#> 4     d     4     2
#> 5     e     5     2

dplyr解决方案,参见David Klotz 的评论

### Klotz's dplyr solution
library(dplyr)
df %>% select_if(function(x) is.character(x) || sum(x) > 5)
#> # A tibble: 5 x 3
#>      id    x2    x3
#>   <chr> <int> <dbl>
#> 1     a     1     2
#> 2     b     2     2
#> 3     c     3     2
#> 4     d     4     2
#> 5     e     5     2
于 2017-10-18T00:00:33.970 回答