I'm creating a new column in my data set that is a copy of a preexisting column but I need to change 2 of the 7 values to something else.
I have tried doing this
Dataset$category_alt = Dataset$category_title
Dataset[Dataset$category_alt == "Shows"] <- "Other"
Dataset[Dataset$category_alt == "Nonprofits & Activism"] <- "Other"
But I receive this error
Error in [<-.data.frame
(*tmp*
, USvideos$category_alt == "Shows", value = "Other") :
duplicate subscripts for columns
CodePudding user response:
You're missing the new column name in the assignment.
Dataset$category_alt = Dataset$category_title
Dataset$category_alt[Dataset$category_alt == "Shows"] <- "Other"
Dataset$category_alt[Dataset$category_alt == "Nonprofits & Activism"] <- "Other"
CodePudding user response:
We can also use %in%
to make the code clearer and shorter
Dataset$category_alt[Dataset$category_alt %in% c('Shows', 'Nonprofits & Activism')] <-'Other'
I usually prefer to use dplyr in such situations:
library(dplyr)
Dataset %>% mutate(category_alt = replace(category_alt, category_alt %in% c('Shows', 'Nonprofits & Activism'), 'Other))