-2

When I run the code

cols_name <- c(10:15)

for(i in 1:ncol(df_churn[,cols_name]))
{
    df_churn[,cols_name][,i] <- as.factor(mapvalues + (df_churn[,cols_name][,i], from =c("No internet service"),to=c("No")))
}

I get the error


Error in mapvalues(df_churn[, cols_name][, i], from = c("No internet service"),  : 
  `x` must be an atomic vector.
Kalana
  • 5,631
  • 7
  • 30
  • 51
  • It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Aug 26 '20 at 04:48

1 Answers1

0

It seems as though you are attempting to replace all "No internet service" responses in columns 10:15 of your dataset with "No"?

Without adjusting your syntax too much try this:

cols_index <- 10:15

for(i in cols_index){
    df_churn[,i] <- as.factor(mapvalues(df_churn[,i], from ="No internet service",to="No"))
}

Let me know how it goes!

statnewb
  • 16
  • 2
  • cols_index <- 10:15 for(i in cols_index){ + churn[,i] <- as.factor(mapvalues(churn[,i], from ="No internet service",to="No")) + } Error in mapvalues(churn[, i], from = "No internet service", to = "No") : `x` must be an atomic vector. – Pravin Nadar Aug 26 '20 at 06:51
  • It is difficult to help without an example I can replicate. Unfortunately I can't reproduce df_churn. Perhaps if you tell me the R output from running class(df_churn) and str(df_churn[10:15]) that might give me some more information – statnewb Aug 26 '20 at 07:00