如何根据列中特定的字符串重新塑造或转置数据框在R中?

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

How to reshape or transpose a data frame based off specific strings in columns in R?

问题

I have a data frame like this

  1. test <- data.frame(matrix(nrow = 18, ncol = 3))
  2. colnames(test) <- c("subject","session","f1")
  3. test$subject <- c(1,1,1,2,2,2,3,3,3,1,1,1,2,2,2,3,3,3)
  4. test$session <- c(1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2)
  5. test$f1 <- c(29,52,72,42,50,52,30,49,63,51,37,43,1,3,2,1,2,0)

I would like it to look like this

  1. test <- data.frame(matrix(nrow=6,ncol = 5))
  2. colnames(test) <- c("subject","session","t1","t2","t3")
  3. test$subject <- c(1,2,3,1,2,3)
  4. test$session <- c(1,1,1,2,2,2)
  5. test$t1 <- c(29,42,30,51,1,1)
  6. test$t2 <- c(52,50,49,37,3,2)
  7. test$t3 <- c(72,52,63,43,2,0)

How would I go about changing it?

英文:

I have a data frame like this

  1. test <- data.frame(matrix(nrow = 18, ncol = 3))
  2. colnames(test) <- c("subject","session","f1")
  3. test$subject <- c(1,1,1,2,2,2,3,3,3,1,1,1,2,2,2,3,3,3)
  4. test$session <- c(1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2)
  5. test$f1 <- c(29,52,72,42,50,52,30,49,63,51,37,43,1,3,2,1,2,0)

I would like it to look like this

  1. test <- data.frame(matrix(nrow=6,ncol = 5))
  2. colnames(test) <- c("subject","session","t1","t2","t3")
  3. test$subject <- c(1,2,3,1,2,3)
  4. test$session <- c(1,1,1,2,2,2)
  5. test$t1 <- c(29,42,30,51,1,1)
  6. test$t2 <- c(52,50,49,37,3,2)
  7. test$t3 <- c(72,52,63,43,2,0)

How would I go about changing it?

答案1

得分: 2

以下是您要翻译的内容:

We could use just pivot_wider() with names_prefix() argument:
as proposed by @Martin Gal without unnest:

  1. library(tidyr)
  2. library(dplyr)
  3. test %>%
  4. mutate(rn = row_number(), .by = c(subject, session)) %>%
  5. pivot_wider(names_from = "rn",
  6. values_from = "f1",
  7. names_prefix = "t_")
  1. library(tidyr)
  2. library(dplyr)
  3. test %>%
  4. mutate(row = row_number(), .by = c(subject, session)) %>%
  5. pivot_wider(names_from = row, values_from = f1,
  6. names_prefix = "t", names_sort = TRUE,
  7. values_fn = list) %>%
  8. unnest(cols = starts_with("t"))
  9. subject session t1 t2 t3
  10. 1 1 1 29 52 72
  11. 2 2 1 42 50 52
  12. 3 3 1 30 49 63
  13. 4 1 2 51 37 43
  14. 5 2 2 1 3 2
  15. 6 3 2 1 2 0
英文:

We could use just pivot_wider() with names_prefix() argument:
as proposed by @Martin Gal without unnest:

  1. library(tidyr)
  2. library(dplyr)
  3. test %>%
  4. mutate(rn = row_number(), .by = c(subject, session)) %>%
  5. pivot_wider(names_from = "rn",
  6. values_from = "f1",
  7. names_prefix = "t_")
  1. library(tidyr)
  2. library(dplyr)
  3. test %>%
  4. mutate(row = row_number(), .by = c(subject, session)) %>%
  5. pivot_wider(names_from = row, values_from = f1,
  6. names_prefix = "t", names_sort = TRUE,
  7. values_fn = list) %>%
  8. unnest(cols = starts_with("t"))
  9. subject session t1 t2 t3
  10. 1 1 1 29 52 72
  11. 2 2 1 42 50 52
  12. 3 3 1 30 49 63
  13. 4 1 2 51 37 43
  14. 5 2 2 1 3 2
  15. 6 3 2 1 2 0

huangapple
  • 本文由 发表于 2023年4月11日 02:54:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/75979858.html
匿名

发表评论

匿名网友

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

确定