R:根据另一列中的值计算一列中的值的数量

时间:2014-10-29 09:21:51

标签: r

我有一个不明确的问题,所以我希望我能正确解释这个问题。 我正在使用R.我知道循环在R中可能很慢,但对我来说在这种情况下使用for循环是可以的。

我有一个这样的数据框:

    id_A    id_B    id_C    calc_A  calc_B  calc_C  
1   x,z     d       g,f        1        1       5
2   x,y,z   d,e     f          1        2       8
3   y,z     d,e     g          6        7       1

我还有一个名为c('A', 'B', 'C', etc.)的向量 我想要做的是计算每一行,有多少idcalc< = 2。 id_Acalc_A等相关联。

例如,对于第一行A和B具有calc值< = 2,A和B一起具有3 id' s。 所以输出将是这样的:

   count
1   3
2   5
3   1

3 个答案:

答案 0 :(得分:3)

这有点乱,但这应该可以解决问题(对于data.frame d):

# store indices of calc columns and id columns
calc.cols <- grep('^calc', names(d))
id.cols <- grep('^id', names(d))

sapply(split(d, seq_len(nrow(d))), function(x) {
  length(unique(unlist(strsplit(paste(x[, id.cols][which(x[, calc.cols] <= 2)], 
                                      collapse=','), ','))))
})

# 1 2 3 
# 3 5 1

答案 1 :(得分:1)

假设ID列和calc列的顺序相同

 library(stringr)
 indx <- sapply(df[,1:3], str_count, ",")+1
 indx[df[,4:6] >2] <- NA
 df$count <- rowSums(indx,na.rm=TRUE)
 df
 #   id_A id_B id_C calc_A calc_B calc_C count
 #1   x,z    d  g,f      1      1      5     3
 #2 x,y,z  d,e    f      1      2      8     5
 #3   y,z  d,e    g      6      7      1     1

更新

假设您的数据集的顺序不一样

 set.seed(42)
 df1 <- df[,sample(6)]
 library(gtools)
 df2 <-df1[,mixedorder(names(df1))]
 #    calc_A calc_B calc_C  id_A id_B id_C
 #1      1      1      5   x,z    d  g,f
 #2      1      2      8 x,y,z  d,e    f
 #3      6      7      1   y,z  d,e    g

 id1 <- grep("^id", colnames(df2))
 calc1 <- grep("^calc", colnames(df2)) 

 indx1 <-sapply(df2[, id1], str_count, ",")+1
 indx1[df2[, calc1] >2] <- NA
 df1$count <- rowSums(indx1, na.rm=TRUE)
 df1
 #     calc_C calc_B id_B id_C calc_A  id_A count
 #1      5      1    d  g,f      1   x,z     3
 #2      8      2  d,e    f      1 x,y,z     5
 #3      1      7  d,e    g      6   y,z     1

数据

df <- structure(list(id_A = c("x,z", "x,y,z", "y,z"), id_B = c("d", 
 "d,e", "d,e"), id_C = c("g,f", "f", "g"), calc_A = c(1L, 1L, 
 6L), calc_B = c(1L, 2L, 7L), calc_C = c(5L, 8L, 1L)), .Names = c("id_A", 
"id_B", "id_C", "calc_A", "calc_B", "calc_C"), class = "data.frame", row.names = c("1", 
"2", "3"))

答案 2 :(得分:0)

我不知道这是不是比jbaums解决方案更乱,但这是另一种选择:

mydf<-data.frame(id_A=c("x,y","x,y,z","y,z"),id_B=c("d","d,e","d,e"),id_C=c("g,f","f","g"),
                 calc_A=c(1,1,6),calc_B=c(1,2,7),calc_C=c(5,8,1),stringsAsFactors=F)



mydf$count<-apply(mydf,1,function(rg,namesrg){
                     rg_calc<-rg[grep("calc",namesrg)]
                     rg_ids<-rg[grep("id",namesrg)]
                     idsinf2<-which(as.numeric( rg_calc)<=2)
                     ttids<-unlist(sapply(rg_ids[gsub("calc","id",names(rg_calc[idsinf2]))],function(id){strsplit(id,",")[[1]]}))
                     return(length(ttids))
                    },colnames(mydf))


>  mydf
   id_A id_B id_C calc_A calc_B calc_C count
1   x,y    d  g,f      1      1      5     3
2 x,y,z  d,e    f      1      2      8     5
3   y,z  d,e    g      6      7      1     1