-1

I have a dataframe with the following format:

time emg eda
Stimulus 80 NA NA
NA NA NA
Onset time MAX (ch4) MAX (ch7)
131.84450000000001 8.6929632579550682 7.8114367391085029
142.83000000000001 2.062198261793945 9.0520476734366273
150.60149999999999 2.8645742403188507 8.6158402220299344
NA NA NA
Stimulus 10 NA NA
NA NA NA
Onset time MAX (ch4) MAX (ch7)
134.25999999999999 1.4936528696982108 8.6331626452321366

This table goes on for about 10 more stimuli, and the amount of rows per stimulus is variable. Is there a way in which I can move the rows that belong to specific stimuli to new columns? I want to do it in such a way that if I get another one of these dataframes, that I can run the code again and format the table without having to check how many rows each stimulus has.

The way I want the table to be formatted is going to be like this:

time emg80 eda80 emg10 eda10
131.84450000000001 8.6929632579550682 7.8114367391085029 NA NA
134.25999999999999 NA NA 1.4936528696982108 8.6331626452321366
142.83000000000001 2.062198261793945 9.0520476734366273 NA NA
150.60149999999999 2.8645742403188507 8.6158402220299344 NA NA

I don't even know if this is possible, but I'd really appreciate anyone who can help me out somewhat.

user2974951
  • 9,535
  • 1
  • 17
  • 24
  • Welcome to Stack Overflow! Please follow the following link and include a reproducible example in your question with proper formatting: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Shibaprasadb Jul 11 '22 at 09:15

1 Answers1

0

Not the most elegant way... using reshape2

df=structure(list(time = c("Stimulus 80", NA, "Onset time", "131.84450000000001", 
"142.83000000000001", "150.60149999999999", NA, "Stimulus 10", 
NA, "Onset time", "134.25999999999999"), emg = c(NA, NA, "MAX (ch4)", 
"8.6929632579550682", "2.062198261793945", "2.8645742403188507", 
NA, NA, NA, "MAX (ch4)", "1.4936528696982108"), eda = c(NA, NA, 
"MAX (ch7)", "7.8114367391085029", "9.0520476734366273", "8.6158402220299344", 
NA, NA, NA, "MAX (ch7)", "8.6331626452321366")), class = "data.frame", row.names = c(NA, 
-11L))

library(reshape2)

df1=melt(
  df,
  id.vars="time",
  measure.vars=c("emg","eda")
)

tmp=grepl("Stimulus",df1$time)

df1$g=cumsum(tmp)

df1=merge(
  df1,
  setNames(
    df1[tmp,c("time","g")],
    c("name","g")
  ),
  by="g",
  all=T
)

df1$name=paste0(
  df1$variable,
  sapply(strsplit(df1$name," "),"[[",2)
)

dcast(
  df1[grepl("^\\d+",df1$time),],
  time~name,
  value.var="value"
)

resulting in

                time              eda10              eda80              emg10              emg80
1 131.84450000000001               <NA> 7.8114367391085029               <NA> 8.6929632579550682
2 134.25999999999999 8.6331626452321366               <NA> 1.4936528696982108               <NA>
3 142.83000000000001               <NA> 9.0520476734366273               <NA>  2.062198261793945
4 150.60149999999999               <NA> 8.6158402220299344               <NA> 2.8645742403188507
user2974951
  • 9,535
  • 1
  • 17
  • 24