简体   繁体   English

长到宽格式 R

[英]Long to Wide format R

I have following data frame and I am not able to get into the wide format.我有以下数据框,但无法进入宽格式。

My Data is like :我的数据是这样的:

cust_acc <- c('A','A','A','A','A','A','A','A','A','A')
quantity<- c(10,20,30,40,50,60,70,80,90,100)
category<-c('pa','we','we','pa','we','pa','we','we','pa','we')
group<- c('1','1','2','2','2','2','2','2','2','2')

df <- as.data.frame(cbind(cust_acc,quantity,category,group))



cust_acc quantity category group
  A       10       pa     1
  A       20       we     1
  A       30       we     2
  A       40       pa     2
  A       50       we     2
  A       60       pa     2
  A       70       we     2
  A       80       we     2
  A       90       pa     2
  A      100       we     2

I am trying to get the output as :我试图获得输出为:

enter code here Output enter code here输出

A  1 PA  we
A  2 we  pa we pa we we pa we

Any help is highly appreciated.任何帮助都受到高度赞赏。

Regards, Fina问候, 菲娜

you can try你可以试试

library(tidyverse)
df %>% 
  group_by(group) %>% 
  mutate(n = 1:n()) %>% 
  select(-quantity) %>% 
  spread(n, category)
# A tibble: 2 x 10
# Groups:   group [2]
  cust_acc group `1`   `2`   `3`   `4`   `5`   `6`   `7`   `8`  
  <fct>    <fct> <fct> <fct> <fct> <fct> <fct> <fct> <fct> <fct>
1 A        1     pa    we    NA    NA    NA    NA    NA    NA   
2 A        2     we    pa    we    pa    we    we    pa    we   

Base R solution:基础 R 解决方案:

# Aggregate the dataframe: 

df_new <- aggregate(category~cust_acc+group,

                    df[,sapply(df, is.character)],

                    paste, collapse = " ")


# Spread out the dataframe, giving NA values if not present: 

df_new <- cbind(cust_acc = df_new$cust_acc, group = df_new$group,

                setNames(data.frame(do.call(rbind,

                                            lapply(strsplit(df_new$category, '\\s+'),

                                                   function(x){

                                                     length(x) = max(lengths(strsplit(df_new$category, '\\s+')))

                                                   return(x)

                                                   }
                                            )
                )
                ),

                c(paste0("category_", 1:max(lengths(strsplit(df_new$category, '\\s+'))))))

)

df_new

Data:数据:

cust_acc <- c('A','A','A','A','A','A','A','A','A','A')
quantity<- c(10,20,30,40,50,60,70,80,90,100)
category<-c('pa','we','we','pa','we','pa','we','we','pa','we')
group<- c('1','1','2','2','2','2','2','2','2','2')

df <- data.frame(cbind(cust_acc,quantity,category,group), stringsAsFactors = FALSE)

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

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