[R] Beginner question: select cases
Hello all, I hope i chose the right list as my question is a beginner-question. I have a data set with 3 colums "London", "Rome" and "Vienna" - the location is presented through a 1 like this: London RomeVienna q1 0 0 1 4 0 1 0 2 1 0 0 3 I just want to calculate the means of a variable q1. I tried following script: # calculate the mean of all locations results <- subset(results, subset== 1 ) mean(results$q1) # calculate the mean of London results <- subset(results, subset== 1 , select=c(London)) mean(results$q1) # calculate the mean of Rome results <- subset(results, subset== 1 , select=c(Rome)) mean(results$q1) # calcualate the mean of Vienna results <- subset(results, subset== 1 , select=c(Vienna)) mean(results$q1) As all results are 1.68 and there is defenitely a difference in the three locations I wonder whats going on. I get confused as the Rcmdr asks me to overwrite things and there is no "just filter" option. Any help would be apprechiated. Thank you in advance. Regards Peter ___CURE - Center for Usability Research & Engineering___ Peter Wolkerstorfer Usability Engineer Hauffgasse 3-5, 1110 Wien, Austria [Tel] +43.1.743 54 51.46 [Fax] +43.1.743 54 51.30 [Mail] [EMAIL PROTECTED] [Web] http://www.cure.at __ R-help@stat.math.ethz.ch 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.
[R] Beginner Loop Question with dynamic variable names
Dear all, I have another small scripting-beginner problem which you hopefully can help: I compute new variables with: # Question 1 results$q1 <- with(results, q1_1*1+ q1_2*2+ q1_3*3+ q1_4*4+ q1_5*5) # Question 2 results$q2 <- with(results, q2_1*1+ q2_2*2+ q2_3*3+ q2_4*4+ q2_5*5) # Question 3 results$q3 <- with(results, q3_1*1+ q3_2*2+ q3_3*3+ q3_4*4+ q3_5*5) # Question 4 results$q4 <- with(results, q4_1*1+ q4_2*2+ q4_3*3+ q4_4*4+ q4_5*5) This is very inefficient so I would like to do this in a loop like: for (i in 1:20) {results$q1 <- with(results, q1_1*1+ q1_2*2+ q1_3*3+ q1_4*4+ q1_5*5)} My question now: How to replace the "1"-s (results$q1, q1_1...) in the variables with the looping variable? Here like I like it (just for illustration - of course I still miss the function to tell R that it should append the value of i to the variable name): # i is the number of questions - just an illustration, I know it does not work this way for (i in 1:20) {results$qi <- with(results, qi_1*1+ qi_2*2+ qi_3*3+ qi_4*4+ qi_5*5)} Help would be greatly appreciated. Thanks in advance. Peter ___CURE - Center for Usability Research & Engineering___ Peter Wolkerstorfer Usability Engineer Hauffgasse 3-5, 1110 Wien, Austria [Tel] +43.1.743 54 51.46 [Fax] +43.1.743 54 51.30 [Mail] [EMAIL PROTECTED] [Web] http://www.cure.at __ R-help@stat.math.ethz.ch 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.