How do I pivot groups of columns of related data in R?

I have a data set where each observation has 3 variables: time, success (yes or no), damage( yes or no). but each participant was try 3 times each of the 3 different methods (A,B,C).

But the organization of the data is the worst I could imagine: The data was collected in a way that each row is one participant, but I need each row to be a different observation.

The raw data looks something like this: enter image description here

And I want something like this:

enter image description here

I tried by using pivot longer() but this works only for individual columns, if I do it here I just get repetitions of observations and the overlap and get all scrambled.

Upvotes: 0

Views: 53

Answers (1)

Evgeniia
Evgeniia

Reputation: 21

I think, you should do something like this:

I have created an example data frame similar to yours:

library(tidyverse)

df <- data.frame(
  c(1, 2), 
  c("M", "F"), 
  c(10, 20), 
  c(15, 25),
  c(12, 13), c("yes", "no"), c("yes", "no"),
  c(22, 25), c("yes", "no"), c("no", "yes"),
  c(55, 40), c("no", "yes"), c("yes", "no"),
  c(39, 68), c("yes", "no"), c("yes", "no")
)

colnames(df) <- 
  c("participant", "Gender", "P. info 1", "P. info 2",
    "time A1", "success A1", "demage A1",
    "time A2", "success A2", "demage A2",
    "time B1", "success B1", "demage B1",
    "time B2", "success B2", "demage B2")

Some gather/spread manipulations and you receive desired output:

df <- df %>%
  gather(key = "Experiment", value = "Value", -c(1:4)) %>% 
  separate(col = "Experiment",
           into = c("Measure", "Method")) %>% 
  separate(col = "Method",
           into = c("Method", "# try"), sep = 1) %>% 
  spread(key = "Measure", value = "Value")

Upvotes: 1

Related Questions