[英]Calculate column wise sum for each row & add new columns for each value in dataframe
I have dataframe as below 我有如下数据框
A B C D
11 11 33 44
11 11 22 33
22 22 33 44
22 33 44 11
I have such thousands of rows . 我有成千上万的行。 Dataframe has only these four values 11, 22,33,44. 数据框仅具有这四个值11、22、33、44。
No NA or blank values 无NA或空白值
I want to take count for each of these values across every row and add them under new column. 我想对每一行中的每个值进行计数,并将其添加到新列下。
I want output as below - 我想要输出如下-
A B C D 11 22 33 44
11 11 33 44 2 0 1 1
11 11 22 33 2 1 1 0
22 22 33 44 0 2 1 1
22 33 44 11 1 1 1 1
Currently i am trying as 目前我正在尝试
count.22 <- sapply(dff,FUN=function(x,dff){sum(dff[,2]==22)},dff)
but its not working. 但它不起作用。
We can use mtabulate
我们可以使用mtabulate
library(qdapTools)
df2 <- cbind(df1, mtabulate(as.data.frame(t(df1))))
row.names(df2) <- NULL
df2
# A B C D 11 22 33 44
#1 11 11 33 44 2 0 1 1
#2 11 11 22 33 2 1 1 0
#3 22 22 33 44 0 2 1 1
#4 22 33 44 11 1 1 1 1
Or we can use apply
from base R
to loop over the rows 或者我们可以使用apply
从base R
遍历行
cbind(df1, t( apply(df1, 1, function(x) table(factor(x, levels = c(11, 22, 33, 44))))))
If your data.frame is x
you can do it as follows: 如果您的data.frame是x
,则可以执行以下操作:
vals <- c(11,22,33,44)
cbind(x, setNames(lapply(vals, function(v) rowSums(x == v)), vals))
# A B C D 11 22 33 44
#1 11 11 33 44 2 0 1 1
#2 11 11 22 33 2 1 1 0
#3 22 22 33 44 0 2 1 1
#4 22 33 44 11 1 1 1 1
This way you don't loop over the rows but the unique values you want to check. 这样,您就不会在行上循环,而是要检查的唯一值。
A similar version with direct assignment would be 直接分配的类似版本是
x[,as.character(vals)] <- lapply(vals, function(v) rowSums(x == v))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.