Hi so I am having issues looping through my data frame and removing columns based on the condition that suppress = 1. So the loop would need to go through every column of df1 and remove the columns suppress = 1 for that same variable. It would need to determine that the specific row of suppress = 1 has the same variable in both df's.
So there are two data frames. df1 contains all the data and df2 contains the conditions based on the variables of df1.
df1 <- c("ID" = c(1,2,3,4,5), "Age" = c(19,50,46,32,28))
df2 <- c("Variable" = c("ID", "Age"), "Suppress" = c(1,0))
The main issue I am having is that the loop I currently have works for when I make a data frame such as df1 and df2, but not for when I import a csv file and use that data.
Could it be the format of the data frames or does the loop need to be adjusted to work for the csv imports? I suspect the latter.
Here is the loop I currently have:
for(i in names(df1)){
if(df2$Variable == names(df1[i]) & df2$Suppress == 1){
df[i] <- NULL
}
}
Another version... essentially the same
for(i in names(df1)){
if(df2$Variable %in% names(df1[i]) & df2$Suppress == 1){
df[i] <- NULL
}
}
I cannot post a csv here, but I recommend trying to run the above code with an imported csv file similar to df1 and df2.
Note: Both the df1 and df2 are being imported as a csv file.
Recap: Why does the current loop not work with imported csv data and what are alternative ways to removing the columns based on df2's suppress variable.
Thanks
Aucun commentaire:
Enregistrer un commentaire