Thanks Josh for the code to post I have been trying to figure out how to do that. Your code works except that it changes subjects that responded with "1 & 2" to all 1's. What does the "ave" argument mean in the execution of the function?
library(car) foo <- function(x) { if (any(grepl(4, x))) { x <- recode(x, "2 = 1; 4 = 2") } return(x) } ## do it dat$test <- with(dat, *ave*(Slide1_RESP, Subject, FUN = foo)) On Fri, Aug 5, 2011 at 4:56 PM, Joshua Wiley <jwiley.ps...@gmail.com> wrote: > On Fri, Aug 5, 2011 at 2:34 PM, Edward Patzelt <patze...@umn.edu> wrote: > > > > The problem is that we were using a task where some subjects responded > with "1 & 2" and some responded with "2 & 4". So there is overlap for 2 > because it means stimulus 1 for subject 1 and it means stimulus 2 for > subject 2. > > > > subject > > > > subject_1 > > > > 1 > > > > subject_1 > > > > 1 > > > > subject_1 > > > > 1 > > > > subject_1 > > > > 2 > > > > subject_1 > > > > 2 > > > > subject_2 > > > > 4 > > > > subject_2 > > > > 2 > > > > subject_2 > > > > 2 > > > > subject_2 > > > > 4 > > > > subject_2 > > > > 2 > > > > subject_2 > > > > 2 > > > > subject_2 > > > > 2 > > > > subject_2 > > > > 4 > > FYI providing data in the format above (this list is plain text) makes > the job of those trying to help substantially harder. You can use > dput(). For example, if I wanted to share the first 10 rows of the > built in mtcars data set, I would just copy and paste the output from > running: > > dput(mtcars[1:10, ]) > > Anyway, here you go, this should be directly executable as long as you > have installed the 'car' package. > > ## your data in a form easily copied and pasted into the console > ## created using dput() (highly recommended for future posts) > dat <- structure(list(subject = c("subject_1", "subject_1", "subject_1", > "subject_1", "subject_1", "subject_2", "subject_2", "subject_2", > "subject_2", "subject_2", "subject_2", "subject_2", "subject_2" > ), val = c(1, 1, 1, 2, 2, 4, 2, 2, 4, 2, 2, 2, 4)), .Names = c("subject", > "val"), row.names = c(NA, -13L), class = "data.frame") > > ## load the required package for recoding > ## though it is overkill for only two levels > require(car) > > ## define a function to do the recoding > foo <- function(x) { > if (any(grepl(4, x))) { > x <- recode(x, "2 = 1; 4 = 2") > } > return(x) > } > > ## do it > dat$altval <- with(dat, ave(val, subject, FUN = foo)) > > Cheers, > > Josh > > > > > > On Fri, Aug 5, 2011 at 4:25 PM, Joshua Wiley <jwiley.ps...@gmail.com> > wrote: > >> > >> Hi Edward, > >> > >> You can try something like: > >> > >> u.ppl <- unique(init.dat1$grid) > >> l.ppl <- ifelse(grepl(4, init.dat1$Slide1_RESP), 2, > >> init.dat1$Slide1_RESP) > >> > >> Note that this is not exact as you have not provided a reproducible > >> example. I am not exactly sure how you are putting 1 for 2 and 2 for > >> 4, if the value is equal to 4, but presumably it is clearer with data. > >> In any event, look at ?ifelse it is something like a vectorized if > >> statement and is, I believe, preferable to your use of a for loop. I > >> can probably give you a runnable solution if you can give the first > >> few rows of the relevant data. > >> > >> Cheers, > >> > >> Josh > >> > >> On Fri, Aug 5, 2011 at 2:15 PM, Edward Patzelt <patze...@umn.edu> > wrote: > >> > I want to write code that says "If you find an element equal to 4 in > this > >> > vector for each person in the data set tested separately, then put in > 1 for > >> > 2 and 2 for 4, else leave the variable as is" > >> > > >> > u.ppl <- (unique(init.dat1$grid)) > >> > l.ppl <- length(u.ppl) > >> > for (i in 1:l.ppl) > >> > { > >> > if (grep("4",init.dat1$Slide1_RESP)) {2 == 1, 4 == 2}; else > >> > init.dat1$Slide1_RESP > >> > > >> > } > >> > > >> > -- > >> > Edward H. Patzelt > >> > Research Assistant TRiCAM Lab > >> > University of Minnesota Psychology/Psychiatry > >> > VA Medical Center > >> > Office: S355 Elliot Hall - Twin Cities Campus > >> > Phone: 612-626-0072 Email: patze...@umn.edu > >> > > >> > Please consider the environment before printing this email > >> > www.psych.umn.edu/research/tricam > >> > > >> > [[alternative HTML version deleted]] > >> > > >> > > >> > ______________________________________________ > >> > R-help@r-project.org mailing list > >> > https://stat.ethz.ch/mailman/listinfo/r-help > >> > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > >> > and provide commented, minimal, self-contained, reproducible code. > >> > > >> > > >> > >> > >> > >> -- > >> Joshua Wiley > >> Ph.D. Student, Health Psychology > >> Programmer Analyst II, ATS Statistical Consulting Group > >> University of California, Los Angeles > >> https://joshuawiley.com/ > > > > > > > > -- > > Edward H. Patzelt > > Research Assistant TRiCAM Lab > > University of Minnesota Psychology/Psychiatry > > VA Medical Center > > Office: S355 Elliot Hall - Twin Cities Campus > > Phone: 612-626-0072 Email: patze...@umn.edu > > > > Please consider the environment before printing this email > > www.psych.umn.edu/research/tricam > > > > -- > Joshua Wiley > Ph.D. Student, Health Psychology > Programmer Analyst II, ATS Statistical Consulting Group > University of California, Los Angeles > https://joshuawiley.com/ > -- Edward H. Patzelt Research Assistant TRiCAM Lab University of Minnesota Psychology/Psychiatry VA Medical Center Office: S355 Elliot Hall - Twin Cities Campus Phone: 612-626-0072 Email: patze...@umn.edu Please consider the environment before printing this email www.psych.umn.edu/research/tricam [[alternative HTML version deleted]]
______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.