R 跨行计算间隔之间的值
我有这个df:R 跨行计算间隔之间的值,r,statistics,sum,R,Statistics,Sum,我有这个df: 如果我正确理解了您的问题: x$less.25 <- apply(x, 1, function(x){sum(x < 25)}) x$between.25_49 <- apply(x, 1, function(x){sum(x >= 25 & x <50)}) x$between.50_74 <- apply(x, 1, function(x){sum(x >= 50 & x <75)}) x$greater.75
如果我正确理解了您的问题:
x$less.25 <- apply(x, 1, function(x){sum(x < 25)})
x$between.25_49 <- apply(x, 1, function(x){sum(x >= 25 & x <50)})
x$between.50_74 <- apply(x, 1, function(x){sum(x >= 50 & x <75)})
x$greater.75 <- apply(x, 1, function(x){sum(x >= 75)})
在列名中使用u而不是-,否则将产生一个错误:x$between.25_49=25&x<50,na.rm=TRUE和x$between.50_74=50&x<75,na.rm=TRUE
x$less.25 <- rowSums(x < 25, na.rm=TRUE)
x$between.25_49 <- rowSums(x >= 25 & x < 50, na.rm=TRUE)
x$between.50_74 <- rowSums(x >= 50 & x < 75, na.rm=TRUE)
x$greater.75 <- rowSums(x >= 75, na.rm=TRUE)
x$less.25 <- apply(x, 1, function(x){sum(x < 25)})
x$between.25_49 <- apply(x, 1, function(x){sum(x >= 25 & x <50)})
x$between.50_74 <- apply(x, 1, function(x){sum(x >= 50 & x <75)})
x$greater.75 <- apply(x, 1, function(x){sum(x >= 75)})
v1 v2 v3 v4 v5 less.25 between.25_49 between.50_74 greater.75
1 99 58 40 10 70 1 1 2 1
2 40 72 49 90 87 0 2 1 2
3 12 76 99 19 71 2 0 1 2
4 7 61 38 20 43 2 2 1 0
5 24 70 62 28 45 1 2 2 0
6 76 37 33 76 83 0 2 0 3