将宽表格转换为长表格在R中

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

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 &lt;- rbinom(n=50,size=1,prob=0.5)
groupB &lt;- rbinom(n=50,size=1,prob=0.5)
groupC &lt;- rbinom(n=50,size=1,prob=0.5)
groupD &lt;- rbinom(n=50,size=1,prob=0.5)
dtTest &lt;- 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 &lt;- dtTest %&gt;% as_tibble() %&gt;% 
  pivot_longer(everything(), names_to = &quot;groups&quot;, values_to = &quot;values&quot;) %&gt;% 
  mutate(answer = ifelse(values == 0, &quot;no&quot;, &quot;yes&quot; ))

答案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 &lt;- stack(as.data.frame(dtTest))[2:1]
names(out) &lt;- c(&quot;Group&quot;, &quot;answer&quot;)
out$answer &lt;- c(&quot;no&quot;, &quot;yes&quot;)[out$answer + 1]

-output

&gt; 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 = &quot;Group&quot;, value.name = &quot;answer&quot;)[, 
  answer := c(&quot;no&quot;, &quot;yes&quot;)[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

huangapple
  • 本文由 发表于 2023年3月4日 03:08:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/75630990.html
匿名

发表评论

匿名网友

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

确定