bkrause404
bkrause404

Reputation: 35

Converting List of Vectors to Data Frame in R

I'm trying to convert a list of vectors into a data frame, with there being a column for Company Names and column for the MPE. My list is generated by running the following code for each company:

MPE[[2]] <- c("Google", abs(((forecasted - goog[nrow(goog),]$close) 
/ goog[nrow(goog),]$close)*100))

Now, i'm having trouble making it into the appropriate data frame for further manipulation. What's the easiest way to do this?

This is an example list of vectors that I would want to manipulate into a dataframe with the company names in one column and the number in the second column.

test <- list(c("Google", 2))
test[[2]] <- c("Microsoft", 3)
test[[3]] <- c("Apple", 4)

Upvotes: 1

Views: 3225

Answers (2)

Matt W.
Matt W.

Reputation: 3722

You can use unlist with matrix and then turn into a dataframe. reducing with rbind could take a long time with a large dataframe I think.

df <- data.frame(matrix(unlist(test), nrow=length(test), byrow=T))
colnames(df) <- c("Company", "MPE")

Upvotes: 3

bkrause404
bkrause404

Reputation: 35

I was actually able to achieve what I wanted with the following:

MPE_df <- data.frame(Reduce(rbind ,MPE))
colnames(MPE_df) <- c("Company", "MPE")
MPE_df

Upvotes: 0

Related Questions