I'm trying to write a function (very new at this) that uses ifelse to compute a new variable, then given this new variable a label and give the levels labels. Then check that it worked by running a frequency and crosstab. I can get it to work if I do it in steps hard coded, but I'm struggling to make it work as a function.
This is what I want the function to do and it works:
df$new_variable<-ifelse(df$other_variable==0,1,0)
label(df$new_variable) <- "Not applicable"
levels(df$Snew_variable) <- list(No=0, Yes=1)
frq(df, new_variable) #see that it created the new variable
cro(df$other_variable, df$new variable) #see that it was created
correctly
This is what I've been trying to make work:
compute_new_var<-function(df, x) {
df$new_variable<-ifelse(x==0,1,0)
label (df$new_variable) <- "Not applicable"
levels(df$new_variable) <-list(No=0, Yes=1)
frq(df, new_variable)
cro(df$new_variable, x)
}
compute_new_var(df_name, other_variable_name)
It's doing a couple of things. It doesn't like "label" so I've been leaving the label and levels lines out. Then it gives me this error that object "new_variable" not found. I'm not sure if I'm leaving something out of the arguments, or if it's not creating new_variable or if it's creating it but not storing it in my df, or if I'm just trying to cram too much in here. Any thoughts?
I would expect it to create the new variable, assign a label to the variable, assign labels to the values, and print a frequency and crosstab.
Aucun commentaire:
Enregistrer un commentaire