1
mydat <- structure(list(A = list(0.0513386405422815, 0.0498556819169294), B = list(0.101635583229926, 0.108375425286815), C = list(0.0505800651643873, 0.0491970061885759), D = list(0.100497645341106, 0.107056699263229)), row.names = 1:2, class = "data.frame") > mydat$A [[1]] [1] 0.05133864 [[2]] [1] 0.04985568 

mydat is a data.frame, but its columns are all in list format. Is there a quick way to convert these to vectors instead?

4 Answers 4

2

You can try

> out <- list2DF(Map(unlist, mydat)) > str(out) 'data.frame': 2 obs. of 4 variables: $ A: num 0.0513 0.0499 $ B: num 0.102 0.108 $ C: num 0.0506 0.0492 $ D: num 0.1 0.107 
Sign up to request clarification or add additional context in comments.

Comments

2

You can unnest all the columns in the dataframe.

library(tidyr) mydat %>% unnest(cols = everything()) # A B C D # <dbl> <dbl> <dbl> <dbl> #1 0.0513 0.102 0.0506 0.100 #2 0.0499 0.108 0.0492 0.107 

Comments

1

Just 'unlist()' them.

lapply(mydat, unlist) $A [1] 0.05133864 0.04985568 $B [1] 0.1016356 0.1083754 $C [1] 0.05058007 0.04919701 $D [1] 0.1004976 0.1070567 

Comments

0

In the same vein as apply()-ing unlist, apply()-ing simplify2array:

# Simplify each list to a vector: data.frame(apply(mydat, 2, simplify2array)) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.