英文:
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 |>
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
data
df <- data.frame(trait = c("Cholesterol", "Cholesterol", "ApoB", "ApoA",
"TRI", "TRI"), 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) > 0) %>%
as.data.frame() %>%
rownames_to_column(var = "trait")
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], "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
<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 <- data.frame(trait = c("Cholesterol", "Cholesterol", "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)))
# 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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论