长到宽格式R

时间:2019-12-04 08:11:07

标签: r

我具有以下数据框,但无法进入宽格式。

我的数据就像:

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

我试图将输出显示为:

enter code here输出

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

我们非常感谢您的帮助。

关于, 菲娜

2 个答案:

答案 0 :(得分:0)

您可以尝试

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   

答案 1 :(得分:0)

Base 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

数据:

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)