Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to use subset() in a for loop in R

I need to select the levels of Species in the dataset Iris (available in R) with the function subset() and calculate the mean of the column Petal.Length from the same dataset, everything with a for loop. I know that I can do this calculations with the function tappy, but the task consists in using a for loop.

I tried writing a vector in which I would put the results:

medie <- rep(NA,3)
names(medie) <- levels(iris$Species)

and then this as the loop:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

  for (i in 1:length(medie)){
    medie[i] <- mean(subset(iris, Species==levels(Species))$Petal.Length)
  }

but this are the results I get:

> medie
    setosa versicolor  virginica 
     3.796      3.796      3.796

Any help?

>Solution :

I think you need to include i in levels(Species)[i]

for (i in 1:length(medie)){
  medie[i] <- mean(subset(iris, Species==levels(Species)[i])$Petal.Length)
}

> medie
    setosa versicolor  virginica 
     1.462      4.260      5.552 

There is an argument called select in subset to select your target column, so you can use:

medie[i] <- mean(subset(iris, Species==levels(Species)[i], select = "Petal.Length"))

Here’s a dplyr approach if you, someday, want to avoid for loop.

library(dplyr)
iris %>% 
  group_by(Species) %>% 
  summarise(medie = mean(Petal.Length))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading