使用循环提取一系列整数 [英] Extracting a series of integers using a loop
问题描述
我有一些数据要提取整数出现的频率.这是一些示例数据:
I have some data where I want to extract the frequency with which the integers appear. Here is some sample data:
df <- read.table(header=T, text="A B C D
1 1 5 3 1
2 1 2 3 2
3 2 3 5 3
4 1 4 5 3
5 3 1 4 2
6 5 2 5 1
")
df
我可以遍历这些并获得如下计数:
I can loop through these and get the counts as follows:
for (i in 1:5){
print(colSums(df==i))
}
但是每次我尝试存储输出时,都会出现错误.将结果输出存储在数据框中的最巧妙的方法是什么?我想我对循环存储数据的方式感到困惑.谢谢你的帮助.
But every time I try to store the output I get an error. What is the neatest way to store the resultant output in a dataframe? I think I'm getting confused about the way to store data that's run through a loop. Thanks for your help.
推荐答案
我们可以使用mtabulate
library(qdapTools)
t(mtabulate(df))
# A B C D
#1 3 1 0 2
#2 1 2 0 2
#3 1 1 2 2
#4 0 1 1 0
#5 1 1 3 0
在base R
中,我们还可以unlist
数据集,复制列名,并使用table
(不使用任何循环,显式(for
)或隐式(lapply
)).
In base R
, we can also unlist
the dataset, replicate the column names, and use table
(not using any loop, explicit (for
) or implicit (lapply
).
table(unlist(df),names(df)[col(df)])
# A B C D
# 1 3 1 0 2
# 2 1 2 0 2
# 3 1 1 2 2
# 4 0 1 1 0
# 5 1 1 3 0
或者就像@nicola提到的那样,我们可以使用rep
(应该更快)代替col(df)
Or as @nicola mentioned, the instead of col(df)
, we can use rep
(should be faster)
table(unlist(df), rep(names(df),each=nrow(df)))
这篇关于使用循环提取一系列整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!