简体   繁体   中英

How can I reshape one column dataframe into 4 columns in R?

Let's suppose I have the following dataframe:

df = data.frame(Value = rnorm(16,0,1), stringsAsFactors = F)

I want to reshape this single column in order to get 4 columns where the first 4 rows of df are the first row of the new df, the rows from 5 to 8 are the second row of the new df, etc.

The desired output would then be:

# original random data

         Value
1   0.91252514
2   0.54858410
3   0.60361002
4  -0.36479957
5  -0.57253378
6  -0.64271622
7   1.06519500
8   1.49557598
9   0.51933447
10  1.04606789
11  0.13745408
12  0.43722826
13 -0.01168782
14 -0.50464089
15 -0.82506265
16 -1.04228086

# desired output

         Col1    Col2              Col3    Col4
1   0.91252514   0.54858410  0.60361002  -0.36479957
2  -0.57253378  -0.64271622   1.06519500   1.49557598
3   0.51933447  1.04606789  0.13745408  0.43722826
4 -0.01168782 -0.50464089 -0.82506265 -1.04228086

I tried this but I can't get what I need:

library(tidyr)

df2 = cbind(data.frame(Group= rep(c(1,2,3,4), length(df)/4),stringsAsFactors = F),df)
df2 %>% separate(Group, c("Beta","SE","t","p"))

Can anyone help me?

Thanks!

Easier with matrix if the number of rows needed is a multiple of the total number of rows in the 'df'

as.data.frame( matrix(df$Value, 4, 4, byrow = TRUE))

Here are some other base R options

> data.frame(t(array(df$Value, c(4, 4))))
           X1         X2         X3         X4
1  0.91252514  0.5485841  0.6036100 -0.3647996
2 -0.57253378 -0.6427162  1.0651950  1.4955760
3  0.51933447  1.0460679  0.1374541  0.4372283
4 -0.01168782 -0.5046409 -0.8250627 -1.0422809

> data.frame(do.call(rbind, split(df$Value, ceiling(seq(nrow(df)) / 4))))
           X1         X2         X3         X4
1  0.91252514  0.5485841  0.6036100 -0.3647996
2 -0.57253378 -0.6427162  1.0651950  1.4955760
3  0.51933447  1.0460679  0.1374541  0.4372283
4 -0.01168782 -0.5046409 -0.8250627 -1.0422809

or (thank @akrun's comment)

t("dim<-"(t(df), c(4, 4)))

With pivot_wider :

library(tidyr)
library(dplyr)

df %>% mutate(id= rep(c(1:4),length.out = nrow(.))) %>%  
       pivot_wider(names_from = id,
                   values_from = Value, 
                   names_prefix = 'Col', 
                   values_fn = list ) %>% 
       unnest(cols = colnames(.))   

# A tibble: 4 x 4
     Col1   Col2   Col3   Col4
    <dbl>  <dbl>  <dbl>  <dbl>
1  0.913   0.549  0.604 -0.365
2 -0.573  -0.643  1.07   1.50 
3  0.519   1.05   0.137  0.437
4 -0.0117 -0.505 -0.825 -1.04 

Similar to @Waldi's approach -

library(dplyr)
library(tidyr)

df %>%
  group_by(id = ceiling(row_number()/4)) %>%
  mutate(row = row_number()) %>%
  pivot_wider(names_from = row, values_from = Value, names_prefix = 'col') %>%
  ungroup %>%
  select(-id)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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