0

Sorry in advance if this seems dumb, still learning R and still learning about classes.

I've created a function that spits back 3 values in list format.

I'm trying to "separate" these values and insert them into a dataframe for use down the line. I want the dataframe to look something like this:

#             | pyro1 | pyro2 | pyro3 |
# baseline0   |       |       |       |
# drop time   |       |       |       |
# drop temp   |       |       |       |
# exo time    |       |       |       |
# baseline1   |       |       |       |
# max temp    |       |       |       |
# baseline2   |       |       |       |

Basically after calling lapply on my function I'm left with a dataframe that looks like this:

test <- data.frame(c(list(10,1025,1000),list(15,1015,1001),list(9,1019,1029)))

Wherein I have three lists, with 3 values each. I need all the first values from each of the lists to go into a specific row of the above template dataframe I made above.

For example:

#             | pyro1 | pyro2 | pyro3 |
# baseline0   | 1025  |  1015 | 1019  |
# drop time   |  10   |  15   | 9     |
# drop temp   | 1000  | 1001  | 1029  |
# exo time    |       |       |       |
# baseline1   |       |       |       |
# max temp    |       |       |       |
# baseline2   |       |       |       |

I've tried using rbind, thinking that would work for some reason, but that doesn't work, instead giving something like this:

#             | pyro1                   | pyro2              | pyro3                |
# baseline0   |list(10.5, 1025, 1023.5) |list(16, 1042, 1036)|list(14.5, 1042, 1038)|
# drop time   |       |       |       |
# drop temp   |       |       |       |
# exo time    |       |       |       |
# baseline1   |       |       |       |
# max temp    |       |       |       |
# baseline2   |       |       |       |

Sorry I don't have such a clear working example, if you need more information please let me know, but I think my problem is somewhat basic (sadly).

Also I may be going about the problem in an 'incorrect' way, will gladly accept any suggestions on a different approach.

Thanks

edit1 After playing around with unlist I've put together some ugly code that gives me the formatting I'm looking for, however I think it might be a very roundabout way of doing things.

testt <- data.frame(c(list(10,1025,1000),list(15,1015,1001),list(9,1019,1029)))
xxx<-data.frame(unlist(testt))
xx1 <- cbind(xxx[1,1],xxx[4,1],xxx[7,1])
xx2 <- cbind(xxx[2,1],xxx[5,1],xxx[8,1])
xx3 <- cbind(xxx[3,1],xxx[6,1],xxx[9,1])
xx4 <- rbind(xx1,xx2,xx3)
xx4
2
  • Look at str(test). test is not what you think it is. Please provide an example of your actual input. dput is handy for that. Commented Jan 30, 2015 at 17:29
  • The output when I dput from my test: dput(test) structure(list(list(10.5, 1025, 1023.5), list(16, 1042, 1036), list(14.5, 1042, 1038)), .Dim = c(1L, 3L), .Dimnames = list( NULL, c("pyro1", "pyro2", "pyro3"))) I think it's different than my example actually Commented Jan 30, 2015 at 17:49

2 Answers 2

3

Your input:

test <- structure(list(list(10.5, 1025, 1023.5), list(16, 1042, 1036), list(14.5, 1042, 1038)), 
                  .Dim = c(1L, 3L), .Dimnames = list( NULL, c("pyro1", "pyro2", "pyro3")))

Use rbind.data.frame and feed it the list using do.call. We set the column names using setNames, but could also use names(...) <-.

setNames(do.call(rbind.data.frame, test), dimnames(test)[[2]])
#   pyro1 pyro2  pyro3
#2   10.5  1025 1023.5
#21  16.0  1042 1036.0
#3   14.5  1042 1038.0
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, this is nearly perfect. I need the values ordered differently however, like above. I just discovered unlist and made some ugly code that give me the format I'm looking for, is there a way to modify the above setNames code to create a format like this:testt <- data.frame(c(list(10,1025,1000),list(15,1015,1001),list(9,1019,1029))) xxx<-data.frame(unlist(testt)) xx1 <- cbind(xxx[1,1],xxx[4,1],xxx[7,1]) xx2 <- cbind(xxx[2,1],xxx[5,1],xxx[8,1]) xx3 <- cbind(xxx[3,1],xxx[6,1],xxx[9,1]) xx4 <- rbind(xx1,xx2,xx3) xx4 Sorry, formatting poor, will edit OP to include this code
I believe you should go back to previous parts of your code instead of trying to fix your mangled data structure.
thanks, sorry I'm not sure how I should set it up. Should my samples (pyro1-3) be in rows? and then the variables be listed in the columns? I I'm just not experienced enough with R to know how I should format them to begin with.
0

In your example the data.frame is bound by columns, not rows, but data.frames are best though of a list of columns where each column has it's own specific meaning and format, but binding lists column-wise is it the transpose of that concept.

It all the values are of the same class (numeric in this exmaple) you could row-bind them together to form a matrix then convert the matrix to a data.frame

mx <- rbind(list(10,1025,1000),list(15,1015,1001),list(9,1019,1029))
df <- as.data.frame(mx)

and if the elements of the lists are not the same class, you could create data frames first and then row-bind them together.

df <- rbind(as.data.frame(list(a =10,b=1025,c=1000)),
            as.data.frame(list(a =15,b=1015,c=1001)),
            as.data.frame(list(a =9,b=1019,c=1029)))

Note that the second method is likely preferable because it preserves the names of the list elements as the filed names in the data.frame. However it does require that that the lists have the same names ('a','b' and 'c', in this case)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.