lundi 23 novembre 2020

Mutate within a for loop

I have a dataframe like this

structure(list(a = c(1, 3, 4, 6, 3, 2, 5, 1), b = c(1, 3, 4, 
2, 6, 7, 2, 6), c = c(6, 3, 6, 5, 3, 6, 5, 3), d = c(6, 2, 4, 
5, 3, 7, 2, 6), e = c(1, 2, 4, 5, 6, 7, 6, 3), f = c(2, 3, 4, 
2, 2, 7, 5, 2)), .Names = c("Love_ABC", "Love_CNN", "Hate_ABC", "Hate_CNN", "Love_CNBC", "Hate_CNBC"), row.names = c(NA, 
8L), class = "data.frame")

I have made the following for loop

channels = c("ABC", "CNN", "CNBC")

for (channel in channels) { 
dataframe <- dataframe %>%
  mutate(ALL_channel = Love_channel + Hate_channel)
  }

But when i run the for loop R tells me " object Love_channel" not found. Have i done something wrong in the for loop?

Aucun commentaire:

Enregistrer un commentaire