1

Ran an experiment with multiple trials, wide format data (i.e., each subject has a row and information pertaining to each of those subject's trials is contained in the columns).

The measurements collected for each trial are identical, such that for each participant there are columns with the same names followed by the relevant trial number e.g., x1, y1, x2, y2 where x and y are measurements collected at each trial and 1 and 2 represent trials 1 and 2 respectively.

I am creating some new variables based on the values for each trial e.g., x1 and y1 should be joined to create x1y1 (the particular functions likely don't matter, as I have been successful in writing them for the first trial). Now I am trying to apply those functions across the multiple trials (which again are identified by the number that follows the variable name) without writing a line of code for each trial 1:n that replicates the code I've written for trial 1. My question is whether I can use apply or a for loop that looks through the column names for the structure/numbering.

Suppose I want to do the following:

XY_1 = paste0(X1,Y1)

but for each trial 1-n. XY_n = paste0(Xn,Yn).

Perhaps something like:

for (trial in c(1:50)){XY[trial] = paste0(X[trial], Y[trial])}

I would like the new variables to be output as columns in the data file. Thank you for your help!

NelsonGon
  • 13,015
  • 7
  • 27
  • 57
Emily
  • 11
  • 1
  • Please edit your question to make it as concise as possible. Also read through [this](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) and improve your question. – NelsonGon Jul 03 '19 at 17:27

1 Answers1

0

I think you're looking for mapply, where you can apply a function to vectors/lists in groups:

v1 <- c("a","b","c")
v2 <- c("1","2","3")

paste_together <- function(X,Y) {
   paste0(X, Y)
}

Called as:

mapply(paste_together, v1, v2)

which gives:

 > result <- mapply(paste_together, v1, v2)
 > result
 a    b    c 
"a1" "b2" "c3" 

or perhaps:

 > result <- mapply(paste_together, v1, v2, USE.NAMES=F)
 > result
"a1" "b2" "c3" 

I wasn't clear on your last statement. If you need the result of this to be in a single column, then convert it into a data frame:

> result <- data.frame(XpasteY=mapply(paste_together, v1, v2, USE.NAMES=F))
> result
  XpasteY
1      a1
2      b2
3      c3
Scott Presnell
  • 1,528
  • 10
  • 23
  • Thank you for the reply. I think you understood the desired output I was describing (to combine cells) and proposed an appropriate function (mapply). Any thoughts on how I could paste together x1 and y1 (i.e., x and y from trial 1), x2 and y2 (i.e., x and y from trial 2), x3 and y3 (i.e., x and y from trial 3), etc. It seems like I should be able to take advantage of the systematicity in the variable names to complete these actions in fewer lines of code than writing the paste function separately for each trial. Hope this helps to clarify, and thank you again for your time in responding! – Emily Jul 05 '19 at 00:17