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

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

convert wide table to long in R

问题

我有一个类似以下的数据集:

  1. groupA <- rbinom(n=50,size=1,prob=0.5)
  2. groupB <- rbinom(n=50,size=1,prob=0.5)
  3. groupC <- rbinom(n=50,size=1,prob=0.5)
  4. groupD <- rbinom(n=50,size=1,prob=0.5)
  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:

  1. groupA &lt;- rbinom(n=50,size=1,prob=0.5)
  2. groupB &lt;- rbinom(n=50,size=1,prob=0.5)
  3. groupC &lt;- rbinom(n=50,size=1,prob=0.5)
  4. groupD &lt;- rbinom(n=50,size=1,prob=0.5)
  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:

  1. library(tidyverse)
  2. library(tidyr)
  3. df <- dtTest %>% as_tibble() %>%
  4. pivot_longer(everything(), names_to = "groups", values_to = "values") %>%
  5. mutate(answer = ifelse(values == 0, "no", "yes"))
英文:

Try tidyverse / tidyr:

  1. library(tidyverse)
  2. library(tidyr)
  3. df &lt;- dtTest %&gt;% as_tibble() %&gt;%
  4. pivot_longer(everything(), names_to = &quot;groups&quot;, values_to = &quot;values&quot;) %&gt;%
  5. 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, ...)

  1. out <- stack(as.data.frame(dtTest))[2:1]
  2. names(out) <- c("Group", "answer")
  3. out$answer <- c("no", "yes")[out$answer + 1]

-output

  1. > head(out)
  2. Group answer
  3. 1 groupA no
  4. 2 groupA no
  5. 3 groupA yes
  6. 4 groupA no
  7. 5 groupA no
  8. 6 groupA no

For data.table::melt, we may specify the measure as the column names, (after converting to data.table - as.data.table)

  1. library(data.table)
  2. melt(as.data.table(dtTest), measure = colnames(dtTest),
  3. variable.name = "Group", value.name = "answer")[,
  4. answer := c("no", "yes")[answer + 1]][]

-output

  1. Group answer
  2. 1: groupA no
  3. 2: groupA no
  4. 3: groupA yes
  5. 4: groupA no
  6. 5: groupA no
  7. ---
  8. 196: groupD yes
  9. 197: groupD yes
  10. 198: groupD yes
  11. 199: groupD yes
  12. 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, ...)

  1. out &lt;- stack(as.data.frame(dtTest))[2:1]
  2. names(out) &lt;- c(&quot;Group&quot;, &quot;answer&quot;)
  3. out$answer &lt;- c(&quot;no&quot;, &quot;yes&quot;)[out$answer + 1]

-output

  1. &gt; head(out)
  2. Group answer
  3. 1 groupA no
  4. 2 groupA no
  5. 3 groupA yes
  6. 4 groupA no
  7. 5 groupA no
  8. 6 groupA no

For data.table::melt, we may specify the measure as the column names, (after converting to data.table - as.data.table)

  1. library(data.table)
  2. melt(as.data.table(dtTest), measure = colnames(dtTest),
  3. variable.name = &quot;Group&quot;, value.name = &quot;answer&quot;)[,
  4. answer := c(&quot;no&quot;, &quot;yes&quot;)[answer + 1]][]

-output

  1. Group answer
  2. 1: groupA no
  3. 2: groupA no
  4. 3: groupA yes
  5. 4: groupA no
  6. 5: groupA no
  7. ---
  8. 196: groupD yes
  9. 197: groupD yes
  10. 198: groupD yes
  11. 199: groupD yes
  12. 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:

确定