计算频率值并保留所有行 - 多个条件

问题描述 投票:1回答:2

我需要考虑整个数据集创建一个频率为两个条件(在每一行中定义)的新列。

请注意,我需要添加此信息并保留以前数据集中的所有行。

例:

library(datasets)
mydata<-CO2
names(mydata)
[1] "Plant"     "Type"      "Treatment" "conc"      "uptake"  

假设我想使用变量'Type'和'Treatment'作为我的条件。因此,我需要计算每一行相应的“类型”和“处理”在整个数据集中出现的次数。

r count frequency
2个回答
3
投票

您可以使用ave来计算每个分组对的长度:

mydata$freq <- ave(rep(1, nrow(mydata)), mydata$Type, mydata$Treatment, FUN = length)

head(mydata)
#  Plant   Type  Treatment conc uptake freq
#1   Qn1 Quebec nonchilled   95   16.0   21
#2   Qn1 Quebec nonchilled  175   30.4   21
#3   Qn1 Quebec nonchilled  250   34.8   21
#4   Qn1 Quebec nonchilled  350   37.2   21
#5   Qn1 Quebec nonchilled  500   35.3   21
#6   Qn1 Quebec nonchilled  675   39.2   21

0
投票

您可以使用dplyr包轻松完成此操作

library(dplyr)
mydata %>% group_by(Type,Treatment) %>% summarize(count = n())

会导致

# A tibble: 4 x 3
# Groups:   Type [?]
         Type  Treatment count
       <fctr>     <fctr> <int>
1      Quebec nonchilled    21
2      Quebec    chilled    21
3 Mississippi nonchilled    21
4 Mississippi    chilled    21
© www.soinside.com 2019 - 2024. All rights reserved.