0

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.

  • 3
    Welcome to Stack Overflow. Please don’t use images of data or code as they cannot be used without a lot of unnecessary effort. [For multiple reasons](//meta.stackoverflow.com/q/285551). You’re more likely to get a positive response if your question is reproducible. [See Stack Overflow question guidance](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – Peter Mar 24 '22 at 16:26
  • Have a look at the `melt` function from `reshape2` package here: https://www.rdocumentation.org/packages/reshape2/versions/1.4.4/topics/melt.data.frame – marcguery Mar 25 '22 at 13:51
  • What do you mean by `tidyr::pivot_longer()` works only for individual columns? Maybe an example of code showing what you tried can help. – Paul Mar 25 '22 at 15:59
  • Please provide enough code so others can better understand or reproduce the problem. – Community Mar 25 '22 at 15:59

1 Answers1

1

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")
Evgeniia
  • 21
  • 3