0

I have a csv called stats with 4 columns, Accepted.Items, A, B, C.

Accepted.Items has rows with regular items: Soda can, banana, orange

While columns A,B and C contain either values 'NA' or 'Yellow'

I want to filter values with 'Yellow' for each A, B and C column and create separate data frames for each of them.

for (i in colnames(stats)) {
  z <- stats[stats$i == "Yellow",]
  z <- data.frame(z$Accepted.Items, z$i)
  print(z)
}
bbqjenny
  • 3
  • 1
  • 1
    I'd strongly recommend putting your data frames in a `list`, i.e., `my_list = list; for(name in c("A", "B", "C")) list[[name]] = stats[stats[[name]] == "Yellow", c("Accepted.Items", name)]`. – Gregor Thomas Apr 30 '19 at 13:36

1 Answers1

0

You cannot reference a column like that, you have to do this instead:

# Wrong
data$i 

# Right
data[i]
Esben Eickhardt
  • 3,183
  • 2
  • 35
  • 56