[英]R: Count number of values from one column according to values in another column
I have a bit of an unclear question, so I hope I can explain this properly. 我有一个不明确的问题,所以我希望我能正确解释这个问题。 I am using R. I know for loops can be slow in R, but for me it would be ok to use a for loop in this case. 我正在使用R.我知道循环在R中可能很慢,但对我来说在这种情况下使用for循环是可以的。
I have a dataframe like this: 我有这样的数据帧:
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
I also have a vector with the names c('A', 'B', 'C', etc.)
What I want to do is to count for every row, how many id
's have a calc
<= 2. id_A
is linked to calc_A
, etc. 我还有一个名为c('A', 'B', 'C', etc.)
的向量c('A', 'B', 'C', etc.)
我想要做的是计算每一行,有多少id
有一个calc
<= id_A
与calc_A
等相关联
For example, for the first row A and B have calc
values <= 2, together A and B have 3 id
's. 例如,对于第一行A和B具有calc
值<= 2,A和B一起具有3个id
。 So the output will be something like this: 所以输出将是这样的:
count
1 3
2 5
3 1
It's a bit messy, but this should do the trick (for data.frame d
): 它有点乱,但这应该可以解决问题(对于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
Assuming that the ID
columns and the calc
columns are in the same order 假设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
Suppose, your dataset is not in the same order 假设您的数据集的顺序不同
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"))
I don't know if this is less messy than jbaums solution but here is another option : 我不知道这是不是比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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.