Carley
Carley

Reputation: 63

Trying to write a function in r that computes a variable and assigns levels and labels

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.

Upvotes: 3

Views: 47

Answers (1)

Jonny Phelps
Jonny Phelps

Reputation: 2727

I had to guess some of the packages as there are multiple versions of some of these functions. Please check :)

I think only issue is that you are inputting a column name character to the function in x - other_variable_name, but you aren't referencing it in the data. So I replaced x with df[[x]] in the function. That's the only major difference.

# please include all packages in the code. Are these right?
library(Hmisc)
library(sjmisc)
library(expss)
set.seed(1)
# table with column called y
df <- data.frame(y=sample(0:3, 10, replace=TRUE))

compute_new_var <- function(df, x){
  # it was missing the reference to df
  df$new_variable <- ifelse(df[[x]]==0, 1, 0)

  # don't need to add package here, just doing it to highlight where they are used
  Hmisc::label(df$new_variable) <- "Not applicable" 
  levels(df$new_variable) <- list(No=0, Yes=1)
  sjmisc::frq(df, new_variable)
  return(expss::cro(df$new_variable, x))
}

compute_new_var(df, x="y")

Upvotes: 2

Related Questions