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

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

how to create a dataframe from two columns in r

问题

我有以下数据集:

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

    trait           ID
    Cholesterol     1
    Cholesterol     2
    ApoB            2
    ApoA            1
    TRI             1
    TRI             2

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

    trait        1     2
    Cholesterol  TRUE  TRUE
    ApoB         FALSE TRUE
    ApoA         TRUE  FALSE
    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))

trait           ID
Cholesterol     1
Cholesterol     2
ApoB            2
ApoA            1
TRI             1
TRI             2

I wanna to create a further dataframe as follow:

trait        1     2
Cholesterol  TRUE  TRUE
ApoB         FALSE TRUE
ApoA         TRUE  FALSE
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。

library(tidyr)

df |>
    pivot_wider(names_from = ID,
                values_from = ID,
                values_fn = as.logical,
                values_fill = FALSE)

# A tibble: 4 × 3
  trait       `1`   `2`  
  <chr>       <lgl> <lgl>
1 Cholesterol TRUE  TRUE 
2 ApoB        FALSE TRUE 
3 ApoA        TRUE  FALSE
4 TRI         TRUE  TRUE 

数据

df <- data.frame(trait = c("Cholesterol", "Cholesterol", "ApoB", "ApoA",
                           "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

library(tidyr)

df |&gt;
    pivot_wider(names_from = ID,
                values_from = ID,
                values_fn = as.logical,
                values_fill = FALSE)

# A tibble: 4 &#215; 3
  trait       `1`   `2`  
  &lt;chr&gt;       &lt;lgl&gt; &lt;lgl&gt;
1 Cholesterol TRUE  TRUE 
2 ApoB        FALSE TRUE 
3 ApoA        TRUE  FALSE
4 TRI         TRUE  TRUE 

data

df &lt;- data.frame(trait = c(&quot;Cholesterol&quot;, &quot;Cholesterol&quot;, &quot;ApoB&quot;, &quot;ApoA&quot;,
                           &quot;TRI&quot;, &quot;TRI&quot;),  ID = c(1,2,2,1,1,2))

</details>



# 答案2
**得分**: 1

你可以尝试 `table`
```R
(table(df) > 0) %>%
    as.data.frame() %>%
    rownames_to_column(var = "trait")

这将得到

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

You can try table

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

which gives

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

答案3

得分: 0

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

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

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

英文:

This is a one-liner in base R:

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

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

答案4

得分: 0

请尝试以下代码:

df <- data.frame(trait = c("胆固醇", "胆固醇", "ApoB", "ApoA", "TRI", "TRI"),
           ID = c(1,2,2,1,1,2)) %>%
  pivot_wider(names_from = ID , values_from = ID, values_fill = FALSE) %>%
  mutate(across(c(`1`,`2`), ~ifelse(.x==0, FALSE, TRUE)))


# 一个 tibble: 4 × 3
  trait       `1`   `2`  
  <chr>       <lgl> <lgl>
1 胆固醇  TRUE  TRUE 
2 ApoB        FALSE TRUE 
3 ApoA        TRUE  FALSE
4 TRI         TRUE  TRUE 

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

英文:

Please try,

df &lt;- data.frame(trait = c(&quot;Cholesterol&quot;, &quot;Cholesterol&quot;, &quot;ApoB&quot;, &quot;ApoA&quot;, &quot;TRI&quot;, &quot;TRI&quot;), 
           ID = c(1,2,2,1,1,2)) %&gt;% 
  pivot_wider(names_from = ID , values_from = ID, values_fill = FALSE) %&gt;% 
  mutate(across(c(`1`,`2`), ~ifelse(.x==0, FALSE, TRUE)))


# A tibble: 4 &#215; 3
  trait       `1`   `2`  
  &lt;chr&gt;       &lt;lgl&gt; &lt;lgl&gt;
1 Cholesterol TRUE  TRUE 
2 ApoB        FALSE TRUE 
3 ApoA        TRUE  FALSE
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:

确定