[英]How to get a a frequency of Ns in R
Here is part of my data这是我的部分数据
df<-read.table (text=" Colour Time1 Time2 Time3 Time4 Time5 Time6
Yellow N Y N N N Y
Red Y N N N N Y
Yellow N N N N N Y
Red N N N Y Y Y
Yellow N N N N N Y
Red Y N N N N Y
Yellow N Y N N Y Y
", header=TRUE)
I want to get the following outcome:我想得到以下结果:
Time Yellow Red
Time1 4 1
Time2 2 3
Time3 4 3
Time4 4 2
Time5 3 2
Time6 0 0
As you can see, I want to count the number of Ns for each time.如您所见,我想计算每次的N个数。 For example, Time 1 has only 4 Ns in Yellow and 1 N in Red.例如,时间 1 黄色只有 4 N,红色只有 1 N。 I think we need to use group_by and then mutate for frequencies of Ns, but I am not clear how to this.我认为我们需要使用 group_by 然后对 Ns 的频率进行变异,但我不清楚如何做到这一点。
An option is to reshape to 'long' format with pivot_longer
on the 'Time' columns, then reshape it back to 'wide' with pivot_wider' while specifying the
values_fn to get the
sum` of logical expression ie count of 'N'一个选项是在“时间”列上使用pivot_longer
重塑为“long”格式,然后使用 pivot_wider 将其重塑为“wide”, pivot_wider' while specifying the
values_fn to get the
总和,即“N”的计数
library(dplyr)
library(tidyr)
df %>%
pivot_longer(cols = starts_with('Time'), names_to = 'Time') %>%
pivot_wider(names_from = Colour, values_from = 'value',
values_fn = function(x) sum(x == 'N'))
-output -输出
# A tibble: 6 x 3
Time Yellow Red
<chr> <int> <int>
1 Time1 4 1
2 Time2 2 3
3 Time3 4 3
4 Time4 4 2
5 Time5 3 2
6 Time6 0 0
Or do a group by summarise
across
the 'Time' columns and then transpose
the output或者across
summarise
“时间”列进行分组,然后transpose
output
library(data.table)
df %>%
group_by(Colour) %>%
summarise(across(everything(), ~ sum(. =='N'))) %>%
data.table::transpose(make.names = 'Colour', keep.names = 'Time')
-output -输出
Time Red Yellow
1 Time1 1 4
2 Time2 3 2
3 Time3 3 4
4 Time4 2 4
5 Time5 2 3
6 Time6 0 0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.