converting as factor for a list of dataframes

I am trying to create a custom function to give labels to modified list of data frames. For example, I have a data frame like below.

  gender = c(1,2,1,2,1,2,1,2,2,2,2,1,1,2,2,2,2,1,1,1,1,1,2,1,2,1,2,2,2,1,2,1,2,1,2,1,2,2,2),
  country = c(3,3,1,2,5,4,4,4,4,3,3,4,3,4,2,1,4,2,3,4,4,4,3,1,2,1,5,5,4,3,1,4,5,2,3,4,5,1,4),

#Labelling is done for categorical variables

df$gender<-factor(df$gender, levels=c(1,2), labels=c("Male","female"))
df$country<-factor(df$country, levels=c(1,2,3,4,5), labels=c("CAN","EU","USA","BR","UK"))
df$Q1<-factor(df$Q1, levels=1, labels="Yes")
df$Q2<-factor(df$Q2, levels=1, labels="Available")
df$Q3<-factor(df$Q3, levels=1, labels="Partner")
df$Q4<-factor(df$Q4, levels=1, labels="Depends on sales")
df$Q5<-factor(df$Q5, levels=c(1,2), labels=c("Local","Overseas"))

Then after defining a list of data frames globally for all chunks

df1<- df
df2<- subset(df, Q5 == 'Local')
df$cc2<-ifelse(df$Q5 == 'Local',1,NA)
lst<-list(df$cc1, df$cc2)
ldat<-list("ALL" = df, "Local" =df2)

Up to this everything is general for all projects

Now in my chunks I have to label data for a list of data frames ( ldat )

I have various variables for labeling to be done like below

df$Q1_new <- df$Q1
df$Q2_new <- df$Q2
df$Q3_new <- df$Q3

Now I am looking for a function by which I can just pass a list of variables for labeling as factor for eg:

variables = c(Q1,Q2,Q3)
new_label =c(Q1_new,Q2_new,Q3_new)

input parameters should be like

funct(dataset = ldat, var=variables,labels= new_label) 

that function will work like below for ldat

df$Q1_new <- df$Q1
df$Q2_new <- df$Q2
df$Q3_new <- df$Q3

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.