英文:
convert wide table to long in R
问题
我有一个类似以下的数据集:
groupA <- rbinom(n=50,size=1,prob=0.5)
groupB <- rbinom(n=50,size=1,prob=0.5)
groupC <- rbinom(n=50,size=1,prob=0.5)
groupD <- rbinom(n=50,size=1,prob=0.5)
dtTest <- cbind(groupA, groupB, groupC, groupD)
其中,0表示"no",1表示"yes"。
现在,我想创建一个表格,包括一列Group
和另一列answer
,指示是或否。
如何实现这个目标?
我尝试使用melt
函数和data.table
的分组,但一直出现错误。
英文:
I have a dataset that look something like this:
groupA <- rbinom(n=50,size=1,prob=0.5)
groupB <- rbinom(n=50,size=1,prob=0.5)
groupC <- rbinom(n=50,size=1,prob=0.5)
groupD <- rbinom(n=50,size=1,prob=0.5)
dtTest <- cbind(groupA, groupB, groupC, groupD)
where 0 means "no" and 1 means "yes".
Now, i want to create a table with a column Group
and another column answer
, stating yes or no.
How can this be done ?
I tried the melt
function and data.table
group by, but keep getting error.
答案1
得分: 1
尝试使用 tidyverse / tidyr:
library(tidyverse)
library(tidyr)
df <- dtTest %>% as_tibble() %>%
pivot_longer(everything(), names_to = "groups", values_to = "values") %>%
mutate(answer = ifelse(values == 0, "no", "yes"))
英文:
Try tidyverse / tidyr:
library(tidyverse)
library(tidyr)
df <- dtTest %>% as_tibble() %>%
pivot_longer(everything(), names_to = "groups", values_to = "values") %>%
mutate(answer = ifelse(values == 0, "no", "yes" ))
答案2
得分: 0
We may use stack
on the data.frame converted - cbind
by default returns a matrix and not a data.frame. Instead, we may directly use data.frame(groupA, groupB, ...)
out <- stack(as.data.frame(dtTest))[2:1]
names(out) <- c("Group", "answer")
out$answer <- c("no", "yes")[out$answer + 1]
-output
> head(out)
Group answer
1 groupA no
2 groupA no
3 groupA yes
4 groupA no
5 groupA no
6 groupA no
For data.table::melt
, we may specify the measure
as the column names, (after converting to data.table - as.data.table
)
library(data.table)
melt(as.data.table(dtTest), measure = colnames(dtTest),
variable.name = "Group", value.name = "answer")[,
answer := c("no", "yes")[answer + 1]][]
-output
Group answer
1: groupA no
2: groupA no
3: groupA yes
4: groupA no
5: groupA no
---
196: groupD yes
197: groupD yes
198: groupD yes
199: groupD yes
200: groupD no
英文:
We may use stack
on the data.frame converted - cbind
by default returns a matrix and not a data.frame. Instead, we may directly use data.frame(groupA, groupB, ...)
out <- stack(as.data.frame(dtTest))[2:1]
names(out) <- c("Group", "answer")
out$answer <- c("no", "yes")[out$answer + 1]
-output
> head(out)
Group answer
1 groupA no
2 groupA no
3 groupA yes
4 groupA no
5 groupA no
6 groupA no
For data.table::melt
, we may specify the measure
as the column names, (after converting to data.table - as.data.table
)
library(data.table)
melt(as.data.table(dtTest), measure = colnames(dtTest),
variable.name = "Group", value.name = "answer")[,
answer := c("no", "yes")[answer + 1]][]
-output
Group answer
1: groupA no
2: groupA no
3: groupA yes
4: groupA no
5: groupA no
---
196: groupD yes
197: groupD yes
198: groupD yes
199: groupD yes
200: groupD no
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论