Expanding a column containing list in a dataframe to a row

Hi,

My source data is this

> x<-c(10,15)
> y<-c(20,33)
> z<-c(4,8)
> a<-data.frame(x,y,z)
> names(a)<-c("F","T","L")

I performed following

> T1<- function(a,b,c){
+     x1<-seq(from=a,to=b,length=c)
+ }
> a$Sam<-mapply(T1,a$F,a$T,a$L)
> a

It results in a dataframe where I guess the column "Sam" contains list.

 F  T L                                                                            Sam
1 10 20 4                                         10.00000, 13.33333, 16.66667, 20.00000
2 15 33 8 15.00000, 17.57143, 20.14286, 22.71429, 25.28571, 27.85714, 30.42857, 33.00000

Is it possible to achieve the following dataframe as the final output
https://drive.google.com/open?id=186gJxpt6MYph1PvWCAQYM9khZGhlfA04

Thank you in advance.

You can use unnest from tidyr. See below:

x <- c(10, 15)
y <- c(20, 33)
z <- c(4, 8)

a <- data.frame(x, y, z)
names(a) <- c("F", "T", "L")

T1 <- function(a, b, c)
{
  x1 <- seq(from = a,
            to = b,
            length = c)
}
a$Sam <- mapply(T1, a$F, a$T, a$L)
a
#>    F  T L
#> 1 10 20 4
#> 2 15 33 8
#>                                                                              Sam
#> 1                                         10.00000, 13.33333, 16.66667, 20.00000
#> 2 15.00000, 17.57143, 20.14286, 22.71429, 25.28571, 27.85714, 30.42857, 33.00000

tidyr::unnest(a, Sam)
#>     F  T L      Sam
#> 1  10 20 4 10.00000
#> 2  10 20 4 13.33333
#> 3  10 20 4 16.66667
#> 4  10 20 4 20.00000
#> 5  15 33 8 15.00000
#> 6  15 33 8 17.57143
#> 7  15 33 8 20.14286
#> 8  15 33 8 22.71429
#> 9  15 33 8 25.28571
#> 10 15 33 8 27.85714
#> 11 15 33 8 30.42857
#> 12 15 33 8 33.00000

Created on 2019-04-06 by the reprex package (v0.2.1)

Hope this helps.

2 Likes

One suggestion:

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
x <- c(10,15)
y <- c(20,33)
z <- c(4,8)
a <- data.frame(x,y,z)
names(a)<-c("F","T","L")

T1<- function(a,b,c){
       x1<-seq(from=a,to=b,length=c)
   }
a$Sam<-mapply(T1,a$F,a$T,a$L)

MakeDF <- function(df) {
  data.frame(F = df[1], T = df[2], L = df[3], Sam = df[4])
}
b <- a %>% rowwise() %>% do(MakeDF(.))
b
#> Source: local data frame [12 x 4]
#> Groups: <by row>
#> 
#> # A tibble: 12 x 4
#>        F     T     L   Sam
#>  * <dbl> <dbl> <dbl> <dbl>
#>  1    10    20     4  10  
#>  2    10    20     4  13.3
#>  3    10    20     4  16.7
#>  4    10    20     4  20  
#>  5    15    33     8  15  
#>  6    15    33     8  17.6
#>  7    15    33     8  20.1
#>  8    15    33     8  22.7
#>  9    15    33     8  25.3
#> 10    15    33     8  27.9
#> 11    15    33     8  30.4
#> 12    15    33     8  33

Created on 2019-04-05 by the reprex package (v0.2.1)

1 Like

@Yarnabrina great stuff. I am a newbie and your solution is easy to understand

Thanks @FJCC for the solution

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.