I am creating several data frames and I want the columns in each of them to be of the same type as the empty data template that I created
For example, I have an empty template
template <- data.frame( char = character(), int = integer(), fac1 = factor(levels = c('level1', 'level2', 'level3')), fac2 = factor(levels = c('level4', 'level5')), stringsAsFactors = FALSE )
And then I want to create several data frames, but I want to save the columns in a template format (i.e. char as a character, fac2 is a factor with two levels level4 and level5)
df1 <- data.frame( char = c('a', 'b'), int = c(1,2), fac1 = c('level2', 'level1'), fac2 = c('level4', 'level4') ) df2 <- data.frame( char = c('c', 'd'), int = c(3,4), fac1 = c('level3', 'level4'), fac2 = c('level5', 'level4') )
I can obviosuly specify the types of columns when I create df1 and df2 , but I want to not type the same thing repeatedly, but if, for example, the levels change in the factor, I only want to change it in one place.
If the value is created in one of the factors that is not level (for example, "level 4" in "fac1" in "df2" is higher, then it should be replaced by NA when converting to the correct format