I know, we can use which() function to subset the data frame according to the given condition.If I have a data frame(dat) with 5 columns and I need to find the mean of 5th column of the rows with w,x,y,z as their 1st,2nd,3rd and 4th column ,then I would do as this-
myrows<-dat[which((dat[,1]==w)&(dat[,2]==x)&(dat[,3]==y)&(dat[,4]==z)),5]
mean(myrows)
Now, if there are 50 such columns then I certainly cannot hard-code as this-
myrows<-dat[which((dat[,1]==w)&(dat[,2]==x)&...&(dat[,49]==zz),50]
mean(myrows)
Is there any other way, something like using a for loop like-
for(i in 1:50)
myrows<-dat[which(dat[,i]==x[i])]
I am a beginner in R, so please give an easiest way around this?