lundi 11 juillet 2016

How to use an if...else statement in R?

I'm trying to create a variable using an if-statement. I want to check whether variable "st" exists in the dataframes in the list of dataframes "dflist", and if it doesn't exist I want to create variable "st". I tried to do it like this(however, it doens't work):

                    #making list of dataframes, and reading them into r
    mylist = list.files(pattern="*.dta")
    dflist <- lapply(mylist, read.dta13)

# if "st" exists in every dataframe in dflist, return "yes", else if it doesn't exist in a particular dataframe, create variable "st" in those dataframes

if(exists(st, dflist)){
    "yes"
  } else{
    st <- c("total")
    dflist$st <- st
  }

Aucun commentaire:

Enregistrer un commentaire