0

Hello i have a data frame which i want to mean some rows. I am using

imc <- dfWoNA[1,]
imc <-head(imc,-1)

id20 <- unique_entrezgene_ids[1:20]

for (j in id20) {
  rows <- dfWoNA [which(dfWoNA$X == as.double(j)),]
  temp <- colMeans(rows[,2:285], na.rm = FALSE)
  nx <- c(j,temp)
  imc <- rbind(imc,nx)

  rm(rows)
  rm(nx)
  rm(temp)
}

shown states however the result of function above i am only taking result for the first line if do the same without for loop manually i am taking result for each of the elements in id20 but for loop gives NA results for the lines except first. If you can help me i appreciate that. Thank you.

alkanschtein
  • 305
  • 1
  • 13
  • In R, 90% of the time you want to make a loop, you shouldn't make a loop. Look into "vectorization." As akrun shows in his answer, this can be done very easily in a two or three lines. Another option would be `rowMeans`. – hmhensen Sep 03 '18 at 06:14

1 Answers1

1

We could do this with tidyverse. Grouped by 'X', get the mean of all columns with summarise_all and bind the rows with the original dataset

library(tidyverse)
dfWoNA %>%
   group_by(X) %>% 
   summarise_all(mean, na.rm = TRUE) %>% 
   bind_rows(dfWoNA, .)
akrun
  • 874,273
  • 37
  • 540
  • 662
  • that's great answer thank you but i have one more question to you how can i learn things like that in R ? is there any book you can suggest ? i am discovering by myself and it is hard to do when i stuck as i searched they are called pipelines the %>% operator how can i become pro in stuff like that ? – alkanschtein Sep 03 '18 at 18:44
  • @alkanschtein Thank you. You can check the resources mentioned [here](https://stackoverflow.com/questions/3375808/learning-r-where-does-one-start). In addition, as in any programming language, practice is the key. So, keep practising for a month and it becomes easier compared to where you started. – akrun Sep 04 '18 at 05:48
  • is there anyway i can show you my whole code to review ? – alkanschtein Sep 04 '18 at 10:50