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)
}