Combine two lists in a dataframe in R

后端 未结 6 2205
青春惊慌失措
青春惊慌失措 2021-02-07 08:46

I have two lists with different structure:

listA <- list(c(\"a\",\"b\",\"c\"), c(\"d\",\"e\"))
listB <- list(0.05, 0.5)

listA
[[1]]
[1] \"a\" \"b\" \"c\"
         


        
6条回答
  •  执念已碎
    2021-02-07 09:48

    Maybe there is a more elegant way that keeps the class numeric of list2's elements... But this one works as well

    df <- do.call(rbind,mapply(cbind, listA, listB))
    df <- as.data.frame(df, stringsAsFactors = FALSE)
    df[,2] <- as.numeric(df[,2])
    

    EDIT Way better is Matthew Plourde's solution using Map aka mapply(data.frame, A=listA, B=listB, SIMPLIFY = FALSE)

提交回复
热议问题