1

Here is the image of the dataframe:

Transposing cells in R dataframe

I have this data frame from a cognitive psych task in which people respond to shapes and sounds. They are meant to always respond in that order, but sometimes they switch but still respond correctly (see second last row for example). I can easily isolate those rows, but I want to transpose the response times so they are in the correct columns. In the example, 1628 which is the response time for the shape stimuli should be in column 'RT1' and the sound response 1462 should be in column 'RT2'.

What function or code can I use to transpose cells given row and column coordinates? Ideally I would use this in the context of an 'if' statement to select the rows in which the responses are transposed.

Hope this makes sense. I cant share the actual data frame.

Kevin Mayo
  • 1,089
  • 6
  • 19
Jay G
  • 25
  • 3
  • Although you can't share the actual data, you could make a small fake data set indicating the problem and also what the intended result is. I don't think you mean transpose, which has a specific meaning in R (and linear algebra). Do you mean switch? – Elin Jul 20 '20 at 02:17
  • To transpose a dataset in R, you could use `t()` as a function – Daniel_j_iii Jul 20 '20 at 02:22
  • Please add data using `dput` and not as images. Please read the info about [how to ask a good question](http://stackoverflow.com/help/how-to-ask) and how to give a [reproducible example](http://stackoverflow.com/questions/5963269). – Ronak Shah Jul 20 '20 at 02:30
  • Thanks - Elin, yes, I mean to switch the cells which represent the RTs which are in the wrong column. Working on making fake data, but that could take some time. Really just want to know how to change particular cells in a data frame – Jay G Jul 20 '20 at 02:40
  • Is the only condition that `Type1 == SOUND`? Do only `RT1` and `RT2` get swapped? What about `Shp.Res` and `Snd.Res`? If so, then `idx <- type1 == "SOUND"` identifies the rows that need to be swapped. – dcarlson Jul 20 '20 at 02:56
  • Thank you. That is helpful. Yes, only RT1 and RT2 get swapped for those trials were Type 1 = Sound. The other columns are not used. – Jay G Jul 20 '20 at 03:04

1 Answers1

1

Assuming your data looks something like this:

dat <- data.frame(
  Type1 = c("SHAPE", "SHAPE", "SOUND", "SHAPE"),
  Type2 = c("SOUND", "SOUND", "SHAPE", "SOUND"),
  RT1 = 10 + 1:4,
  RT2 = 20 + 1:4)

Base R

swaps <- dat$Type1 == "SOUND" & dat$Type2 == "SHAPE"
tmp1 <- dat$Type2[swaps]
dat$Type2[swaps] <- dat$Type1[swaps]
dat$Type1[swaps] <- tmp1
tmp1 <- dat$RT2[swaps]
dat$RT2[swaps] <- dat$RT1[swaps]
dat$RT1[swaps] <- tmp1
dat
#   Type1 Type2 RT1 RT2
# 1 SHAPE SOUND  11  21
# 2 SHAPE SOUND  12  22
# 3 SHAPE SOUND  23  13
# 4 SHAPE SOUND  14  24

dplyr

library(dplyr)
dat %>%
  mutate(
    swaps = Type1 == "SOUND" & Type2 == "SHAPE",
    tmp = if_else(swaps, Type2, Type1),
    Type2 = if_else(swaps, Type1, Type2),
    Type1 = tmp,
    tmp = if_else(swaps, RT2, RT1),
    RT2 = if_else(swaps, RT1, RT2),
    RT1 = tmp
  ) %>%
  select(-tmp)

data.table

library(data.table)
as.data.table(dat)[ Type1 == "SOUND" & Type2 == "SHAPE",
    c("Type1", "Type2", "RT1", "RT2") := .(Type2, Type1, RT2, RT1) ]
r2evans
  • 141,215
  • 6
  • 77
  • 149