简体   繁体   中英

Considering only rows with certain value in a column in R

I have a matrix that looks like this

1 2 8 
2 2 9
3 1 10
4 5 10
5 2 12

Now I want to apply some functions on the third columns of the rows that have a certain value in their second column. So for example I only want to consider the rows that have a 2 in their second column. And then I would like to have the mean, the median and the variance of 8,9,12.

In reality my matrix is larger but this is the basic idea. I guess I need a function that first checks the value in the second column and then considers only the values of the third column that correspond with the given value. I made something like this, but it works just for the mean and is actually a waste since that function is already built in R.

This is what that looked like:

average<- function(m){
  k=0
   for (i in 1:nrow(x)){
     if(x[i,2]==m){
       k = k + x[i,3]
     } else {NULL}
     amount <- sum(x[,2]==m)
  }
   av <- k/amount
  return(av)}
mat <- matrix(c(1:5, 2,2,1,5,2, 8,9,10,10,12), ncol = 3)

mean(mat[, 3][which(mat[, 2] == 2)])

Working from the inside out, first identify the indices corresponding to which of the second columns = 2 which(mat[, 2] == 2) . Then, subset column 3 to those indices with mat[, 3][...] , finally take their mean() .

Something like mean(subset(df,column2name==2)[,3]) ?
Or mean(df$column3name[df$column2name==2]) ?

For pure matrix mean(df[df[,2]==2,3]) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM