Olli
Olli

Reputation: 305

How to split a dataframe and attach the splitted part in new column?

I want to split a dataframe by changing values in the first column and afterward attach the split part in a new column. An example is given below. However, I end up with a list that I can't process back to a handy dataframe.

the desired output should look like df_goal, which is not yet properly formatted.

#data

x <-c(1,2,3)
y <-c(20200101,20200101,20200101)
z <-c(4.5,5,7)
x_name <- "ID"
y_name <- "Date"
z_name <- "value"   

df <-data.frame(x,y,z)
names(df) <- c(x_name,y_name,z_name)

#processing

df$date <-format(as.Date(as.character(df$date), format="%Y%m%d"))
df01 <- split(df, f = df$ID)

#goal
a <-c(1)
b <-c(20200101)
c <-c(4.5)
d <-c(2)
e <-c(20200101)
f <-c(5)
g <-c(3)
h <-c(20200101)
i <-c(7)

df_goal <- data.frame(a,b,c,d,e,f,g,h,i)

Upvotes: 3

Views: 202

Answers (4)

GKi
GKi

Reputation: 39657

You can use Reduce and cbind to cbind each row of a data.frame in one row and keep the type of the columns.

Reduce(function(x,y) cbind(x, df[y,]), 2:nrow(df), df[1,])
#  ID     Date value ID     Date value ID     Date value
#1  1 20200101   4.5  2 20200101     5  3 20200101     7
#Equivalent for the sample dataset: cbind(cbind(df[1,], df[2,]), df[3,])

or do.call with split:

do.call(cbind, split(df, 1:nrow(df)))
#  1.ID   1.Date 1.value 2.ID   2.Date 2.value 3.ID   3.Date 3.value
#1    1 20200101     4.5    2 20200101       5    3 20200101       7
#Equivalent for the sample dataset: cbind(df[1,], df[2,], df[3,])

In case you have several rows per ID you can try:

x <- split(df, df$ID)
y <- max(unlist(lapply(x, nrow)))
do.call(cbind, lapply(x, function(i) i[1:y,]))

Upvotes: 3

tmfmnk
tmfmnk

Reputation: 39858

One option could be:

setNames(Reduce(c, asplit(df, 1)), letters[1:Reduce(`*`, dim(df))])

     a          b          c          d          e          f          g          h          i 
   1.0 20200101.0        4.5        2.0 20200101.0        5.0        3.0 20200101.0        7.0 

Upvotes: 1

yoshi8585
yoshi8585

Reputation: 113

This is a possible solution for your example :

new_df = data.frame(list(df[1,],df[2,],df[3,]))

And if you want to generalize that on a bigger data.frame :

new_list = list()
for ( i in 1:dim(df)[1] ){
    new_list[[i]] = df[i,]
}
new_df = data.frame(new_list)

Upvotes: 1

ThomasIsCoding
ThomasIsCoding

Reputation: 101327

Maybe you can try the following code

df_goal <- data.frame(t(c(t(df))))

such that

> df_goal
  X1       X2  X3 X4       X5 X6 X7       X8 X9
1  1 20200101 4.5  2 20200101  5  3 20200101  7

Upvotes: 0

Related Questions