如何在R中合并多个列表并根据共同变量进行组织

huangapple go评论80阅读模式
英文:

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 &lt;- c(&quot;ACRU&quot;,&quot;AHAA&quot;,&quot;ACHS&quot;,&quot;BLEH&quot;,&quot;HRFW&quot;,&quot;HWHW&quot;, &quot;WBFW&quot;, &quot;ZIMR&quot;)
L2 &lt;- c(&quot;ACRU&quot;,&quot;AHAA&quot;,&quot;ACHS&quot;,&quot;BLEH&quot;)
L3 &lt;- c(&quot;ACRU&quot;,&quot;HRFW&quot;,&quot;HWHW&quot;,&quot;ZIMR&quot;)
L4 &lt;- c(&quot;ACRU&quot;, &quot;AHAA&quot;, &quot;HWHW&quot;, &quot;WBFW&quot;)

df_list &lt;- list(L1, L2, L3, L4)

df_list %&gt;% reduce(full_join, by = &quot;L1&quot;) #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 &lt;- c(&quot;ACRU&quot;,&quot;AHAA&quot;,&quot;ACHS&quot;,&quot;BLEH&quot;,&quot;HRFW&quot;,&quot;HWHW&quot;, &quot;WBFW&quot;, &quot;ZIMR&quot;)
L2 &lt;- c(&quot;ACRU&quot;,&quot;AHAA&quot;,&quot;ACHS&quot;,&quot;BLEH&quot;)
L3 &lt;- c(&quot;ACRU&quot;,&quot;HRFW&quot;,&quot;HWHW&quot;,&quot;ZIMR&quot;)
L4 &lt;- c(&quot;ACRU&quot;, &quot;AHAA&quot;, &quot;HWHW&quot;, &quot;WBFW&quot;)

df &lt;- tibble(
  L1,
  L2 = L1 %in% L2,
  L3 = L1 %in% L3,
  L4 = L1 %in% L4,
)

mutate(df, across(!L1, ~if_else(.x, L1, NA_character_)))
#&gt; # A tibble: 8 &#215; 4
#&gt;   L1    L2    L3    L4   
#&gt;   &lt;chr&gt; &lt;chr&gt; &lt;chr&gt; &lt;chr&gt;
#&gt; 1 ACRU  ACRU  ACRU  ACRU 
#&gt; 2 AHAA  AHAA  &lt;NA&gt;  AHAA 
#&gt; 3 ACHS  ACHS  &lt;NA&gt;  &lt;NA&gt; 
#&gt; 4 BLEH  BLEH  &lt;NA&gt;  &lt;NA&gt; 
#&gt; 5 HRFW  &lt;NA&gt;  HRFW  &lt;NA&gt; 
#&gt; 6 HWHW  &lt;NA&gt;  HWHW  HWHW 
#&gt; 7 WBFW  &lt;NA&gt;  &lt;NA&gt;  WBFW 
#&gt; 8 ZIMR  &lt;NA&gt;  ZIMR  &lt;NA&gt;

<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 &lt;- c(&quot;ACRU&quot;,&quot;AHAA&quot;,&quot;ACHS&quot;,&quot;BLEH&quot;,&quot;HRFW&quot;,&quot;HWHW&quot;, &quot;WBFW&quot;, &quot;ZIMR&quot;)
L2 &lt;- c(&quot;ACRU&quot;,&quot;AHAA&quot;,&quot;ACHS&quot;,&quot;BLEH&quot;)
L3 &lt;- c(&quot;ACRU&quot;,&quot;HRFW&quot;,&quot;HWHW&quot;,&quot;ZIMR&quot;)
L4 &lt;- c(&quot;ACRU&quot;, &quot;AHAA&quot;, &quot;HWHW&quot;, &quot;WBFW&quot;)

df_list &lt;- list(L1, L2, L3, L4)


max_length &lt;- max(lengths(df_list))

lapply(df_list, function(x){
    length(x) &lt;- max_length
    x
}) |&gt;
    as.data.frame() |&gt; 
    setNames(c(&#39;L1&#39;, &quot;L2&quot;, &quot;L3&quot;, &quot;L4&quot;))

    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 &lt;NA&gt; &lt;NA&gt; &lt;NA&gt;
6 HWHW &lt;NA&gt; &lt;NA&gt; &lt;NA&gt;
7 WBFW &lt;NA&gt; &lt;NA&gt; &lt;NA&gt;
8 ZIMR &lt;NA&gt; &lt;NA&gt; &lt;NA&gt;

huangapple
  • 本文由 发表于 2023年3月21日 00:04:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/75792630.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定