I am trying to use a for loop to save dataframes and variable names on the way.
I have a data frame called regionmap, one of the variables (Var3) can take thousands different values, among which there are 15 of this form: "RegionMap *" where * is one of the values of the vector c:
regions <- c("A", "B"........"Z")
I need to run a loop which selects the rows in which each of these values appear, save those rows as a new data frame, transform the relative frequency in a dummy and then merge the new data frame with a bigger one aimed at collecting all of these.
The following code works, I just wanted to know whether it possible to run it 15 times substituting every "A" (both as strings to select and as names of data frames and variables) with other elements of c like in a for loop. (standard for loop does not work)
A <- regionmap[grep("RegionMap A", regionmap$Var3), ]
A$Freq[A$Freq > 1] <- 1
A$Var3 <- NULL
colnames(A) <- c( "name", "date", "RegionMap A")
access_panel <- merge(access_panel, A,by=c("name", "date"))