英文:
How to combine multiple lists in R and organize according to common variable
问题
我已经将实验变量的数据解析成单独的列表,并希望将这些数据合并到一个数据框中,显示哪些变量具有哪些物种集合。您可以使用以下代码来实现:
library(dplyr)
# 创建数据框
df <- data.frame(L1 = L1, L2 = L2, L3 = L3, L4 = L4)
# 用NA填充缺失值
df[is.na(df)] <- "NA"
# 输出结果
print(df)
这将创建您所期望的数据框,如您所描述的那样。
英文:
I have multiple lists in R that each have unique species data according to an experimental setup. I already have the data parsed by experimental variable into separate lists and am looking to combine said data into one dataframe that shows me which variables have which sets of species (example below)
L1 <- c("ACRU","AHAA","ACHS","BLEH","HRFW","HWHW", "WBFW", "ZIMR")
L2 <- c("ACRU","AHAA","ACHS","BLEH")
L3 <- c("ACRU","HRFW","HWHW","ZIMR")
L4 <- c("ACRU", "AHAA", "HWHW", "WBFW")
df_list <- list(L1, L2, L3, L4)
df_list %>% reduce(full_join, by = "L1") #I thought this might organize according to the variable in question, but it does not.
What I am looking to create is:
L1 | L2 | L3 | L4 |
---|---|---|---|
ACRU | ACRU | ACRU | ACRU |
AHAA | AHAA | NA | AHAA |
ACHS | ACHS | NA | NA |
BLEH | BLEH | NA | NA |
HRFW | NA | HRFW | NA |
HWHW | NA | HWHW | HWHW |
WBFW | NA | NA | WBFW |
ZIMR | NA | ZIMR | NA |
Any help would be greatly appreciated
答案1
得分: 1
以下是您提供的代码的翻译部分:
library(tidyverse)
L1 <- c("ACRU", "AHAA", "ACHS", "BLEH", "HRFW", "HWHW", "WBFW", "ZIMR")
L2 <- c("ACRU", "AHAA", "ACHS", "BLEH")
L3 <- c("ACRU", "HRFW", "HWHW", "ZIMR")
L4 <- c("ACRU", "AHAA", "HWHW", "WBFW")
df <- tibble(
L1,
L2 = L1 %in% L2,
L3 = L1 %in% L3,
L4 = L1 %in% L4,
)
mutate(df, across(!L1, ~if_else(.x, L1, NA_character_)))
希望这有所帮助。如果您有任何其他问题,请随时提出。
英文:
library(tidyverse)
L1 <- c("ACRU","AHAA","ACHS","BLEH","HRFW","HWHW", "WBFW", "ZIMR")
L2 <- c("ACRU","AHAA","ACHS","BLEH")
L3 <- c("ACRU","HRFW","HWHW","ZIMR")
L4 <- c("ACRU", "AHAA", "HWHW", "WBFW")
df <- tibble(
L1,
L2 = L1 %in% L2,
L3 = L1 %in% L3,
L4 = L1 %in% L4,
)
mutate(df, across(!L1, ~if_else(.x, L1, NA_character_)))
#> # A tibble: 8 × 4
#> L1 L2 L3 L4
#> <chr> <chr> <chr> <chr>
#> 1 ACRU ACRU ACRU ACRU
#> 2 AHAA AHAA <NA> AHAA
#> 3 ACHS ACHS <NA> <NA>
#> 4 BLEH BLEH <NA> <NA>
#> 5 HRFW <NA> HRFW <NA>
#> 6 HWHW <NA> HWHW HWHW
#> 7 WBFW <NA> <NA> WBFW
#> 8 ZIMR <NA> ZIMR <NA>
<sup>Created on 2023-03-20 with reprex v2.0.2</sup>
答案2
得分: 0
full_join
或任何连接操作仅适用于数据框。您只有向量。您需要通过列绑定您的四个向量而不是循环来创建一个数据框。以下是一种方法。
L1 <- c("ACRU","AHAA","ACHS","BLEH","HRFW","HWHW", "WBFW", "ZIMR")
L2 <- c("ACRU","AHAA","ACHS","BLEH")
L3 <- c("ACRU","HRFW","HWHW","ZIMR")
L4 <- c("ACRU", "AHAA", "HWHW", "WBFW")
df_list <- list(L1, L2, L3, L4)
max_length <- max(lengths(df_list))
lapply(df_list, function(x){
length(x) <- max_length
x
}) |
as.data.frame() |
setNames(c('L1', "L2", "L3", "L4"))
结果如下:
L1 L2 L3 L4
1 ACRU ACRU ACRU ACRU
2 AHAA AHAA HRFW AHAA
3 ACHS ACHS HWHW HWHW
4 BLEH BLEH ZIMR WBFW
5 HRFW <NA> <NA> <NA>
6 HWHW <NA> <NA> <NA>
7 WBFW <NA> <NA> <NA>
8 ZIMR <NA> <NA> <NA>
希望这对您有所帮助。
英文:
full_join
or any join, works only on dataframes. You only have vectors. You need to create a dataframe by column binding your four vectors without recycling. Here's one way to do it.
L1 <- c("ACRU","AHAA","ACHS","BLEH","HRFW","HWHW", "WBFW", "ZIMR")
L2 <- c("ACRU","AHAA","ACHS","BLEH")
L3 <- c("ACRU","HRFW","HWHW","ZIMR")
L4 <- c("ACRU", "AHAA", "HWHW", "WBFW")
df_list <- list(L1, L2, L3, L4)
max_length <- max(lengths(df_list))
lapply(df_list, function(x){
length(x) <- max_length
x
}) |>
as.data.frame() |>
setNames(c('L1', "L2", "L3", "L4"))
L1 L2 L3 L4
1 ACRU ACRU ACRU ACRU
2 AHAA AHAA HRFW AHAA
3 ACHS ACHS HWHW HWHW
4 BLEH BLEH ZIMR WBFW
5 HRFW <NA> <NA> <NA>
6 HWHW <NA> <NA> <NA>
7 WBFW <NA> <NA> <NA>
8 ZIMR <NA> <NA> <NA>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论