簡體   English   中英

如何轉置列表的每個向量,給它們分配一個唯一的數字,並將其綁定到r中?

[英]How can I transpose each vectors of a list assign a unique number to them, and bind them in r?

我有以下列表,並想創建一個data.frame,其中每個列表(有很多)組合在一起。 這里是一個例子:

v11 <- c("was_on_the_moon", "safe", "best", "super")
v22 <- c("no", "yes", "three", "four")
dat1 <- data.frame(cbind(v11, v22))
v11 <- c("was_on_the_moon", "safe", "best", "super")
v22 <- c("no", "yes", "three", "four")
dat2 <- data.frame(cbind(v11, v22))
list_first <- list(dat1, dat2)

結果應如下所示:

  was_on_the_moon safe   best super 
1 yes              yes  three  four
2 no              sure  check  four

tidyverse解決方案:

map(list_first, ~ setNames(., c("var", "val"))) %>%
  bind_rows(.id = "id") %>%
  spread(var, val)
a <- unlist(myfiles, recursive = FALSE)
names <- a$V1
t(sapply(myfiles, function(x) setNames(x$V11, names)[match(names, x$V22)]))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM