New to R, trying to iterate through a DF and see if the data type of the row is empty ('') If so, I would like to add a factor and if not then do nothing.
My expected output is for df$data_type[i] to contain 25 Levels: BIGINT BOOLEAN BYTEINT CHAR DATE DATETIME DECIMAL DOUBLE FLOAT INT NUMBER NUMERIC OBJECT REAL SMALLINT STRING ... VARIANT. I am using this DF to render a Rhandsontable and I need the empty data type cols to have a drop down which contains the items in the factor. The dropdown will be used for users to select only a valid data type from the data_type_vals.
sample df vals
logical_name physical_name data_type length
1 AGENT PERIPHERAL NAME AGT_PRPHL_NM VARCHAR 32.0
2 AGENT SCHEDULE IDENTIFIER AGT_SCHD_ID NUMBER <NA>
3 DUE DATE DUE_DT
foo <- function(df){
data_type_vals <- c("BIGINT", "BOOLEAN", "BYTEINT", "CHAR", "DATE", "DATETIME", "DECIMAL", "DOUBLE", "FLOAT", "INT", "NUMBER", "NUMERIC", "OBJECT", "REAL", "SMALLINT", "STRING", "TEXT", "TIME", "TIMESTAMP", "TIMESTAMP_LTZ", "TIMESTAMP_NTZ", "TIMESTAMP_TZ", "VARBINARY", "VARCHAR", "VARIANT")
df <- mutate_all(df, funs(toupper))
dput(head(df))
if(nrow(df)>=1){
for(i in 1:nrow(df)){
if(df$data_type[i] == ''){
#this line is not working
df$data_type[i] <- factor('', levels = data_type_vals)
#this works but replaces all
df$data_type <- factor('', levels = data_type_vals)
}
}
}
dput(head(df))
}
Rhandsontable image/the red box should be a dropdown with the factor values