[英]How to count the presence of unique elements from each row of data set and list them in 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.