如何在R中从两列创建一个数据框。

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

how to create a dataframe from two columns in r

问题

我有以下数据集:

  1. > data.frame(trait = c("Cholesterol", "Cholesterol", "ApoB", "ApoA",
  2. > "TRI", "TRI"), ID = c(1,2,2,1,1,2))
  3. trait ID
  4. Cholesterol 1
  5. Cholesterol 2
  6. ApoB 2
  7. ApoA 1
  8. TRI 1
  9. TRI 2

我想创建一个进一步的数据框,如下所示:

  1. trait 1 2
  2. Cholesterol TRUE TRUE
  3. ApoB FALSE TRUE
  4. ApoA TRUE FALSE
  5. TRI TRUE TRUE

你知道如何获得这个第二个数据框吗?

英文:

I have the following dataset:

> data.frame(trait = c("Cholesterol", "Cholesterol", "ApoB", "ApoA",
> "TRI", "TRI"), ID = c(1,2,2,1,1,2))

  1. trait ID
  2. Cholesterol 1
  3. Cholesterol 2
  4. ApoB 2
  5. ApoA 1
  6. TRI 1
  7. TRI 2

I wanna to create a further dataframe as follow:

  1. trait 1 2
  2. Cholesterol TRUE TRUE
  3. ApoB FALSE TRUE
  4. ApoA TRUE FALSE
  5. TRI TRUE TRUE

Do you know how to get this second dataframe?

答案1

得分: 3

这是tidyr::pivot_wider()的典型用例。

as.logical会将1或2都强制转换为TRUE,因为values_fill = FALSE,其余的NA值被转换为FALSE。

  1. library(tidyr)
  2. df |>
  3. pivot_wider(names_from = ID,
  4. values_from = ID,
  5. values_fn = as.logical,
  6. values_fill = FALSE)
  7. # A tibble: 4 × 3
  8. trait `1` `2`
  9. <chr> <lgl> <lgl>
  10. 1 Cholesterol TRUE TRUE
  11. 2 ApoB FALSE TRUE
  12. 3 ApoA TRUE FALSE
  13. 4 TRI TRUE TRUE

数据

  1. df <- data.frame(trait = c("Cholesterol", "Cholesterol", "ApoB", "ApoA",
  2. "TRI", "TRI"), ID = c(1,2,2,1,1,2))
英文:

This is the typical case for tidyr::pivot_wider().

as.logical will coerce either 1 or 2 to TRUE, and the remaining NAs are converted to FALSE because of values_fill = FALSE

  1. library(tidyr)
  2. df |&gt;
  3. pivot_wider(names_from = ID,
  4. values_from = ID,
  5. values_fn = as.logical,
  6. values_fill = FALSE)
  7. # A tibble: 4 &#215; 3
  8. trait `1` `2`
  9. &lt;chr&gt; &lt;lgl&gt; &lt;lgl&gt;
  10. 1 Cholesterol TRUE TRUE
  11. 2 ApoB FALSE TRUE
  12. 3 ApoA TRUE FALSE
  13. 4 TRI TRUE TRUE

data

  1. df &lt;- data.frame(trait = c(&quot;Cholesterol&quot;, &quot;Cholesterol&quot;, &quot;ApoB&quot;, &quot;ApoA&quot;,
  2. &quot;TRI&quot;, &quot;TRI&quot;), ID = c(1,2,2,1,1,2))
  3. </details>
  4. # 答案2
  5. **得分**: 1
  6. 你可以尝试 `table`
  7. ```R
  8. (table(df) > 0) %>%
  9. as.data.frame() %>%
  10. rownames_to_column(var = "trait")

这将得到

  1. trait 1 2
  2. 1 ApoA TRUE FALSE
  3. 2 ApoB FALSE TRUE
  4. 3 Cholesterol TRUE TRUE
  5. 4 TRI TRUE TRUE
英文:

You can try table

  1. (table(df) &gt; 0) %&gt;%
  2. as.data.frame() %&gt;%
  3. rownames_to_column(var = &quot;trait&quot;)

which gives

  1. trait 1 2
  2. 1 ApoA TRUE FALSE
  3. 2 ApoB FALSE TRUE
  4. 3 Cholesterol TRUE TRUE
  5. 4 TRI TRUE TRUE

答案3

得分: 0

这是基础R中的一行代码:

  1. cbind(df[1], "1" = df[[2]] == 1, "2" = df[[2]] == 2)
  2. #> trait 1 2
  3. #> 1 Cholesterol TRUE FALSE
  4. #> 2 Cholesterol FALSE TRUE
  5. #> 3 ApoB FALSE TRUE
  6. #> 4 ApoA TRUE FALSE
  7. #> 5 TRI TRUE FALSE
  8. #> 6 TRI FALSE TRUE

创建于2023年05月28日,使用 reprex v2.0.2

英文:

This is a one-liner in base R:

  1. cbind(df[1], &quot;1&quot; = df[[2]] == 1, &quot;2&quot; = df[[2]] == 2)
  2. #&gt; trait 1 2
  3. #&gt; 1 Cholesterol TRUE FALSE
  4. #&gt; 2 Cholesterol FALSE TRUE
  5. #&gt; 3 ApoB FALSE TRUE
  6. #&gt; 4 ApoA TRUE FALSE
  7. #&gt; 5 TRI TRUE FALSE
  8. #&gt; 6 TRI FALSE TRUE

<sup>Created on 2023-05-28 with reprex v2.0.2</sup>

答案4

得分: 0

请尝试以下代码:

  1. df <- data.frame(trait = c("胆固醇", "胆固醇", "ApoB", "ApoA", "TRI", "TRI"),
  2. ID = c(1,2,2,1,1,2)) %>%
  3. pivot_wider(names_from = ID , values_from = ID, values_fill = FALSE) %>%
  4. mutate(across(c(`1`,`2`), ~ifelse(.x==0, FALSE, TRUE)))
  5. # 一个 tibble: 4 × 3
  6. trait `1` `2`
  7. <chr> <lgl> <lgl>
  8. 1 胆固醇 TRUE TRUE
  9. 2 ApoB FALSE TRUE
  10. 3 ApoA TRUE FALSE
  11. 4 TRI TRUE TRUE

注意:只有代码部分被翻译,其他内容保持不变。

英文:

Please try,

  1. df &lt;- data.frame(trait = c(&quot;Cholesterol&quot;, &quot;Cholesterol&quot;, &quot;ApoB&quot;, &quot;ApoA&quot;, &quot;TRI&quot;, &quot;TRI&quot;),
  2. ID = c(1,2,2,1,1,2)) %&gt;%
  3. pivot_wider(names_from = ID , values_from = ID, values_fill = FALSE) %&gt;%
  4. mutate(across(c(`1`,`2`), ~ifelse(.x==0, FALSE, TRUE)))
  5. # A tibble: 4 &#215; 3
  6. trait `1` `2`
  7. &lt;chr&gt; &lt;lgl&gt; &lt;lgl&gt;
  8. 1 Cholesterol TRUE TRUE
  9. 2 ApoB FALSE TRUE
  10. 3 ApoA TRUE FALSE
  11. 4 TRI TRUE TRUE

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

发表评论

匿名网友

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

确定