简体   繁体   English

格式化R分类数据

[英]Formatting data in R categorical

Let

reason <- c("v","v","v","v","v","s","s","s","v","v","v","s","s")
location <- c("c","c","c","c","c","c","c","c","h","h","h","h","h")
zero_one <- c(1,1,0,1,1,1,1,0,1,0,0,1,0)  
df <- data.frame(reason, location, zero_one)

Is there an easy way to convert "df" to "DF", where "DF" has the following shape: 有没有一种简单的方法可以将“ df”转换为“ DF”,其中“ DF”具有以下形状:

reason  location  #zeros  #ones  
     v         c       1      4  
     s         c       1      2  
     v         h       2      1  
     s         h       1      1

You could do this using dcast 您可以使用dcast进行此操作

library(reshape2)
dcast(transform(df, zero_one= factor(zero_one, levels=0:1,
  labels=c('zeros', 'ones'))), ...~zero_one, value.var='zero_one', length)
#   reason location zeros ones
#1      s        c     1    2
#2      s        h     1    1
#3      v        c     1    4
#4      v        h     2    1

Or using data.table (similar approach as @jalapic's) 或使用data.table (与data.table相似的方法)

setDT(df)[,list(zeros=sum(!zero_one), ones=sum(!!zero_one)),
            .(reason, location)][]
#   reason location zeros ones
#1:      v        c     1    4
#2:      s        c     1    2
#3:      v        h     2    1
#4:      s        h     1    1

Or in base R 或以base Rbase R

 aggregate(cbind(zeros=!zero_one, ones=!!zero_one)~., df, FUN= sum)
 #  reason location zeros ones
 #1      s        c     1    2
 #2      v        c     1    4
 #3      s        h     1    1
 #4      v        h     2    1

You can do it with dplyr very simply: 您可以使用dplyr

library(dplyr)
df %>% 
 group_by(reason,location) %>% 
 summarize(zeros = sum(zero_ones==0), ones = sum(zero_ones==1))

#  reason location zeros ones
#1      s        c     1    2
#2      s        h     1    1
#3      v        c     1    4
#4      v        h     2    1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM