Hello R users, I'm new to R so apologies if this question seems simple. I have a matrix which is 35000 columns by 35000 rows and I?m wanting to work out the mean of each row. I've tried using the code below on a smaller version of the matrix, but receive an error:> mat????? [,1]????? [,2] [,3]????? [,4]?????? [,5]????? [,6]????? [,7]????? [,8]????? [,9]???? [,10]???? [,11] ?[1,]?? NA 0.3904762? 0.2 0.4285714 0.04761905??????? NA??????? NA??????? NA??????? NA??????? NA??????? NA ?[2,]?? NA??????? NA? 0.2 0.2000000 0.23809524 0.2190476??????? NA??????? NA??????? NA??????? NA??????? NA ?[3,]?? NA??????? NA?? NA 0.5047619 0.27619048 0.2952381 0.1428571??????? NA??????? NA??????? NA??????? NA ?[4,]?? NA??????? NA?? NA??????? NA 0.42857143 0.3714286 0.3333333 0.2190476??????? NA??????? NA??????? NA ?[5,]?? NA??????? NA?? NA??????? NA???????? NA 0.8666667 0.6761905 0.4857143 0.6571429??????? NA??????? NA ?[6,]?? NA??????? NA?? NA??????? NA???????? NA??????? NA 0.6190476 0.4285714 0.6761905 0.4857143??????? NA ?[7,]?? NA??????? NA?? NA??????? NA???????? NA??????? NA??????? NA 0.6952381 0.6380952 0.6000000 0.2571429 ?[8,]?? NA??????? NA?? NA??????? NA???????? NA??????? NA??????? NA??????? NA 0.6761905 0.6000000 0.1809524 ?[9,]?? NA??????? NA?? NA??????? NA???????? NA??????? NA??????? NA??????? NA??????? NA 0.6190476 0.3142857 [10,]?? NA??????? NA?? NA??????? NA???????? NA??????? NA??????? NA??????? NA??????? NA??????? NA 0.5809524 [11,]?? NA??????? NA?? NA??????? NA???????? NA??????? NA??????? NA??????? NA??????? NA??????? NA??????? NA> mean.matrix <- matrix(ncol=1, nrow=10) > for (i in mat)+ mean.matrix[,i] <- apply(mat,1,mean) Error: NAs are not allowed in subscripted assignments I have come across the na.omit() function however this completely removes the row. Is there any way I can either remove all the NAs from the matrix or pull out the values of each row to work out the mean? Thanks, Amie
Hi, Try: ?rowMeans rowMeans(mat,na.rm=TRUE) A.K. On Sunday, December 1, 2013 1:15 PM, Amie Hunter <amie_hunter at hotmail.com> wrote: Hello R users, I'm new to R so apologies if this question seems simple. I have a matrix which is 35000 columns by 35000 rows and I?m wanting to work out the mean of each row. I've tried using the code below on a smaller version of the matrix, but receive an error:> mat????? [,1]????? [,2] [,3]????? [,4]?????? [,5]????? [,6]????? [,7]????? [,8]????? [,9]???? [,10]???? [,11] ?[1,]?? NA 0.3904762? 0.2 0.4285714 0.04761905??????? NA??????? NA??????? NA??????? NA??????? NA??????? NA ?[2,]?? NA??????? NA? 0.2 0.2000000 0.23809524 0.2190476??????? NA??????? NA??????? NA??????? NA??????? NA ?[3,]?? NA??????? NA?? NA 0.5047619 0.27619048 0.2952381 0.1428571??????? NA??????? NA??????? NA??????? NA ?[4,]?? NA??????? NA?? NA??????? NA 0.42857143 0.3714286 0.3333333 0.2190476??????? NA??????? NA??????? NA ?[5,]?? NA??????? NA?? NA??????? NA???????? NA 0.8666667 0.6761905 0.4857143 0.6571429??????? NA??????? NA ?[6,]?? NA??????? NA?? NA??????? NA???????? NA??????? NA 0.6190476 0.4285714 0.6761905 0.4857143??????? NA ?[7,]?? NA??????? NA?? NA??????? NA???????? NA??????? NA??????? NA 0.6952381 0.6380952 0.6000000 0.2571429 ?[8,]?? NA??????? NA?? NA??????? NA???????? NA??????? NA??????? NA??????? NA 0.6761905 0.6000000 0.1809524 ?[9,]?? NA??????? NA?? NA??????? NA???????? NA??????? NA??????? NA??????? NA??????? NA 0.6190476 0.3142857 [10,]?? NA??????? NA?? NA??????? NA???????? NA??????? NA??????? NA??????? NA??????? NA??????? NA 0.5809524 [11,]?? NA??????? NA?? NA??????? NA???????? NA??????? NA??????? NA??????? NA??????? NA??????? NA??????? NA> mean.matrix <- matrix(ncol=1, nrow=10) > for (i in mat)+ mean.matrix[,i] <- apply(mat,1,mean) Error: NAs are not allowed in subscripted assignments I have come across the na.omit() function however this completely removes the row. Is there any way I can either remove all the NAs from the matrix or pull out the values of each row to work out the mean? Thanks, Amie ??? ??? ??? ? ??? ??? ? ______________________________________________ R-help at 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.
Hello, Your code corrected would be mean.matrix[, 1] <- apply(mat, 1, mean, na.rm = TRUE) (No need for the for loop). Even better would be to avoid loops and use the base R function ?rowMeans. mean.matrix[, 1] <- rowMeans(mat, na.rm = TRUE) Hope this helps, Rui Barradas Em 01-12-2013 12:16, Amie Hunter escreveu:> Hello R users, > > I'm new to R so apologies if this question seems simple. I have a matrix which is 35000 columns by 35000 rows and I?m wanting to work out the mean of each row. I've tried using the code below on a smaller version of the matrix, but receive an error: > >> mat > [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] > [1,] NA 0.3904762 0.2 0.4285714 0.04761905 NA NA NA NA NA NA > [2,] NA NA 0.2 0.2000000 0.23809524 0.2190476 NA NA NA NA NA > [3,] NA NA NA 0.5047619 0.27619048 0.2952381 0.1428571 NA NA NA NA > [4,] NA NA NA NA 0.42857143 0.3714286 0.3333333 0.2190476 NA NA NA > [5,] NA NA NA NA NA 0.8666667 0.6761905 0.4857143 0.6571429 NA NA > [6,] NA NA NA NA NA NA 0.6190476 0.4285714 0.6761905 0.4857143 NA > [7,] NA NA NA NA NA NA NA 0.6952381 0.6380952 0.6000000 0.2571429 > [8,] NA NA NA NA NA NA NA NA 0.6761905 0.6000000 0.1809524 > [9,] NA NA NA NA NA NA NA NA NA 0.6190476 0.3142857 > [10,] NA NA NA NA NA NA NA NA NA NA 0.5809524 > [11,] NA NA NA NA NA NA NA NA NA NA NA >> mean.matrix <- matrix(ncol=1, nrow=10) >> for (i in mat) > + mean.matrix[,i] <- apply(mat,1,mean) > Error: NAs are not allowed in subscripted assignments > > I have come across the na.omit() function however this completely removes the row. Is there any way I can either remove all the NAs from the matrix or pull out the values of each row to work out the mean? > > Thanks, > > Amie > ______________________________________________ > R-help at 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. >