R中的行比率

时间:2014-12-25 16:36:44

标签: r dataframe row

我使用R来分析调查。下面描述了一个示例数据帧。我想在DF中创建两个新列。第一个称为DF $比率,应该找到每个组的TRUE到FALSE的比率。因此,例如,新列的第二行应为0.676(23/34),第四行应为0.622(204/328)。第二个,称为DF $ ratio2,应该是每个组的TRUE数与TRUE和FALSE之和的比率。因此,例如,新列的第二行应为0.404(23 /(23 + 34))。我是R.的新手。有一种简单的方法可以生成这些新列吗?谢谢!

   accept                         group number
1   FALSE                 Very positive     34
2    TRUE                 Very positive     23
3   FALSE             Somewhat positive    328
4    TRUE             Somewhat positive    204
5   FALSE Neither positive nor negative    469
6    TRUE Neither positive nor negative    207
7   FALSE             Somewhat negative    225
8    TRUE             Somewhat negative    111
9   FALSE                 Very negative     76
10   TRUE                 Very negative     34
11  FALSE           Not Sure/Don’t Know    306
12   TRUE           Not Sure/Don’t Know     63

3 个答案:

答案 0 :(得分:2)

我建议重塑您的数据:

DF <- read.table(text='   accept                         group number
1   FALSE                 "Very positive"     34
2    TRUE                 "Very positive"     23
3   FALSE             "Somewhat positive"    328
4    TRUE             "Somewhat positive"    204
5   FALSE "Neither positive nor negative"    469
6    TRUE "Neither positive nor negative"    207
7   FALSE             "Somewhat negative"    225
8    TRUE             "Somewhat negative"    111
9   FALSE                 "Very negative"     76
10   TRUE                 "Very negative"     34
11  FALSE           "Not Sure/Don’t Know"    306
12   TRUE           "Not Sure/Don’t Know"     63', header=TRUE)

library(reshape2)
DF.wide <- dcast(DF, group ~ accept, value.var="number")

DF.wide$ratio <- DF.wide[["TRUE"]]/DF.wide[["FALSE"]]
DF.wide$ratio2 <- DF.wide[["TRUE"]]/(DF.wide[["TRUE"]] + DF.wide[["FALSE"]])
#                           group FALSE TRUE     ratio    ratio2
# 1 Neither positive nor negative   469  207 0.4413646 0.3062130
# 2           Not Sure/Don’t Know   306   63 0.2058824 0.1707317
# 3             Somewhat negative   225  111 0.4933333 0.3303571
# 4             Somewhat positive   328  204 0.6219512 0.3834586
# 5                 Very negative    76   34 0.4473684 0.3090909
# 6                 Very positive    34   23 0.6764706 0.4035088

答案 1 :(得分:1)

你可以尝试

library(dplyr)
DF %>%
    group_by(group) %>% 
    mutate(ratio=number[accept]/number[!accept],
               ratio2=number[accept]/sum(number))

或者

library(data.table)
setDT(DF)[,c('ratio', 'ratio2'):= list(number[accept]/number[!accept], 
              ratio2=number[accept]/sum(number)) , by=group] 

或者,如果我们假设每个rows有两个group,并且它被排序为FALSETRUE,并且您想填充NAratio

ratio2accept=FALSE
 DF$ratio[DF$accept] <- with(DF, number[accept]/number[!accept])
 DF$ratio2[DF$accept] <- with(DF, number[accept]/(number[!accept]+
                                                    number[accept]))     
  DF
  #   accept                         group number     ratio    ratio2
  #1   FALSE                 Very positive     34        NA        NA
  #2    TRUE                 Very positive     23 0.6764706 0.4035088
  #3   FALSE             Somewhat positive    328        NA        NA
  #4    TRUE             Somewhat positive    204 0.6219512 0.3834586
  #5   FALSE Neither positive nor negative    469        NA        NA
  #6    TRUE Neither positive nor negative    207 0.4413646 0.3062130
  #7   FALSE             Somewhat negative    225        NA        NA
  #8    TRUE             Somewhat negative    111 0.4933333 0.3303571
  #9   FALSE                 Very negative     76        NA        NA
  #10   TRUE                 Very negative     34 0.4473684 0.3090909
  #11  FALSE           Not Sure/Don’t Know    306        NA        NA
  #12   TRUE           Not Sure/Don’t Know     63 0.2058824 0.1707317

答案 2 :(得分:0)

比率1:

DF$ratio[c(FALSE, TRUE)] <- DF$number[c(FALSE, TRUE)] / DF$number[c(TRUE, FALSE)]

比率2:

DF$ratio2[c(FALSE, TRUE)] <- DF$number[c(FALSE, TRUE)] / (DF$number[c(TRUE, FALSE)]+DF$number[c(FALSE,TRUE)])