0

I have a series of if else statements that I want to apply to my data to create dummy variables. I want to apply it to a list of columns, swapping out imagetag_object_video with the new name. My list has about 10 strings that I want to replace.

Here is the individual list of if else statements:

new_df2$imagetag_object_video_Position_Center <- ifelse((rowSums(new_df2[,startsWith(names(new_df2), "imagetag_object_video_position")]=="CENTER", na.rm=TRUE) >= 1)==TRUE ,1,0)
new_df2$imagetag_object_video_Position_Center_Right <- ifelse((rowSums(new_df2[,startsWith(names(new_df2), "imagetag_object_video_position")]=="CENTER_RIGHT", na.rm=TRUE) >= 1)==TRUE ,1,0)
new_df2$imagetag_object_video_Position_Center_Left <- ifelse((rowSums(new_df2[,startsWith(names(new_df2), "imagetag_object_video_position")]=="CENTER_LEFT", na.rm=TRUE) >= 1)==TRUE ,1,0)
new_df2$imagetag_object_video_Position_Top_Center <- ifelse((rowSums(new_df2[,startsWith(names(new_df2), "imagetag_object_video_position")]=="TOP_CENTER", na.rm=TRUE) >= 1)==TRUE ,1,0)
new_df2$imagetag_object_video_Position_Top_Right <- ifelse((rowSums(new_df2[,startsWith(names(new_df2), "imagetag_object_video_position")]=="TOP_RIGHT", na.rm=TRUE) >= 1)==TRUE ,1,0)
new_df2$imagetag_object_video_Position_Top_Left <- ifelse((rowSums(new_df2[,startsWith(names(new_df2), "imagetag_object_video_position")]=="TOP_LEFT", na.rm=TRUE) >= 1)==TRUE ,1,0)
new_df2$imagetag_object_video_Position_Bottom_Center <- ifelse((rowSums(new_df2[,startsWith(names(new_df2), "imagetag_object_video_position")]=="BOTTOM_CENTER", na.rm=TRUE) >= 1)==TRUE ,1,0)
new_df2$imagetag_object_video_Position_Bottom_Right <- ifelse((rowSums(new_df2[,startsWith(names(new_df2), "imagetag_object_video_position")]=="BOTTOM_RIGHT", na.rm=TRUE) >= 1)==TRUE ,1,0)
new_df2$imagetag_object_video_Position_Bottom_Left <- ifelse((rowSums(new_df2[,startsWith(names(new_df2), "imagetag_object_video_position")]=="BOTTOM_LEFT", na.rm=TRUE) >= 1)==TRUE ,1,0)

new_df2 <- select(new_df2, -contains("imagetag_object_video_position", ignore.case=FALSE))

I try putting it into a for loop but i get the error:

Error in new_df2$paste0(x, "_Position_Center") <- ifelse((rowSums(new_df2[,  : 
  invalid function in complex assignment
position_list<- list(imagetag_object_video, imagetag_logo_video, textvalue_video)

position_list2 <- for (x in position_list) {
  new_df2$paste0(x,"_Position_Center") <- ifelse((rowSums(new_df2[,startsWith(names(new_df2), paste0(x,"_position"))]=="CENTER", na.rm=TRUE) >= 1)==TRUE ,1,0)
  new_df2$paste0(x,"_Position_Center_Right") <- ifelse((rowSums(new_df2[,startsWith(names(new_df2), paste0(x,"_position"))]=="CENTER_RIGHT", na.rm=TRUE) >= 1)==TRUE ,1,0)
  new_df2$paste0(x,"_Position_Center_Left") <- ifelse((rowSums(new_df2[,startsWith(names(new_df2), paste0(x,"_position"))]=="CENTER_LEFT", na.rm=TRUE) >= 1)==TRUE ,1,0)
  new_df2$paste0(x,"_Position_Top_Center") <- ifelse((rowSums(new_df2[,startsWith(names(new_df2), paste0(x,"_position"))]=="TOP_CENTER", na.rm=TRUE) >= 1)==TRUE ,1,0)
  new_df2$paste0(x,"_Position_Top_Right") <- ifelse((rowSums(new_df2[,startsWith(names(new_df2), paste0(x,"_position"))]=="TOP_RIGHT", na.rm=TRUE) >= 1)==TRUE ,1,0)
  new_df2$paste0(x,"_Position_Top_Left") <- ifelse((rowSums(new_df2[,startsWith(names(new_df2), paste0(x,"_position"))]=="TOP_LEFT", na.rm=TRUE) >= 1)==TRUE ,1,0)
  new_df2$paste0(x,"_Position_Bottom_Center") <- ifelse((rowSums(new_df2[,startsWith(names(new_df2), paste0(x,"_position"))]=="BOTTOM_CENTER", na.rm=TRUE) >= 1)==TRUE ,1,0)
  new_df2$paste0(x,"_Position_Bottom_Right") <- ifelse((rowSums(new_df2[,startsWith(names(new_df2), paste0(x,"_position"))]=="BOTTOM_RIGHT", na.rm=TRUE) >= 1)==TRUE ,1,0)
  new_df2$paste0(x,"_Position_Bottom_Left") <- ifelse((rowSums(new_df2[,startsWith(names(new_df2), paste0(x,"_position"))]=="BOTTOM_LEFT", na.rm=TRUE) >= 1)==TRUE ,1,0)
  }
Jess
  • 15
  • 2
  • 1
    You can't use `$` with variable column names, use `[[` instead. Using the built-in `mtcars` data as an example, if you have `col <- "mpg"`, then `mtcars$col` does not work, but `mtcars[[col]]` does. – Gregor Thomas Nov 04 '22 at 17:43
  • So wherever you have `new_df$paste0(...)` change to `new_df[[paste0(...)]]` – Gregor Thomas Nov 04 '22 at 17:44
  • I am trying to create these new columns, im getting the error: Error in ``[[<-`(`*tmp*`, i, value = value) : no such index at level 1` – Jess Nov 04 '22 at 17:48
  • Well, that I can't help with without a little bit of sample data. Could you share 5 rows and 2 or 3 columns to illustrate the problem? Something like `dput(new_df[1:5, 2:3])`, choosing a few rows and columns that illustrate the problem. – Gregor Thomas Nov 04 '22 at 17:49
  • A side note, `ifelse(condition == TRUE, 1, 0)` can be simplified a little to `ifelse(condition, 1, 0)`. If condition is `TRUE`, you don't need `== TRUE` to make it more true. And I think your code would be much more readable if you did `cols <- startsWith(names(new_df2), paste0(x,"_position"))` once inside the loop, and then used, e.g., `rowSums(new_df2[, cols] == "CENTER", na.rm = TRUE) >= 1` for each condition. – Gregor Thomas Nov 04 '22 at 17:52
  • (Please share desired output as well as the corresponding input) – Gregor Thomas Nov 04 '22 at 17:54

0 Answers0