1

I have a series of variables that fall under one related question: lets say there are 20 such variables in my dataframe, each one corresponds to an option on a MC question. They are titled popn1, popn2......popn20. I want to label each variable by its option, as an example: (popn1 = Everyone; popn2=Children)

I'm using the labelVector package.

Is there a way I can do it without writing out each variable name? Ex. is there a paste function I can use, such as

df2 <- Set_label(df1,
(paste0(popn, 1:20) =  "Everyone", "Children", .... "Youth"?)
Pre
  • 111
  • 7

1 Answers1

1

This can be done in base R quite easily. Here's some sample data (using columns instead of 20, to make it easier to view)

     popn1    popn2    popn3    popn4    popn5
1 -0.4085141 3.240716 2.730837 6.428722 8.015210
2  3.1378943 2.512700 2.021546 3.333371 5.654401
3  2.4073278 1.475619 2.449742 2.817447 6.295569

It looks like you already have your new column names in a character vector:

your_column_names <- c("Everyone", "Youth", "Someone", "Something", "Somewhere")

Then you just use the setNames argument on the column names for your data:

colnames(data) <- setNames(your_column_names, colnames(data))

   Everyone    Youth  Someone Something Somewhere
1 -0.4085141 3.240716 2.730837  6.428722  8.015210
2  3.1378943 2.512700 2.021546  3.333371  5.654401
3  2.4073278 1.475619 2.449742  2.817447  6.295569

Sample Data:

    data <- structure(list(popn1 = c(-0.408514139489243, 3.13789432899688, 
2.40732780606037), popn2 = c(3.24071608151551, 2.51269963339946, 
1.47561933493116), popn3 = c(2.73083728435832, 2.02154567048998, 
2.44974180329751), popn4 = c(6.42872215439841, 3.3333709733048, 
2.81744655980154), popn5 = c(8.0152099281755, 5.65440141443164, 
6.29556905855252)), class = "data.frame", row.names = c(NA, -3L
))
Matt
  • 7,255
  • 2
  • 12
  • 34
  • I'm sorry the dataset is much longer - it's 375 columns. Can I set variable labels using a column number? That might be be easier. I do want to keep them as labels instead of variable names, as I've already set the variable names for each of the columns. – Pre Nov 18 '20 at 01:33
  • Do you have your new column names organized in an Excel doc or something? – Matt Nov 18 '20 at 01:38
  • Yes, I created variable names in Excel and added them to the dataset. It was the best way I could think of for getting variable names that would be easy to use when I'm trying to manipulate the data. I want to add the labels to the variables that will not be manipulated (ie will not becombined under one column)- there are a few sets. – Pre Nov 18 '20 at 01:41
  • If you have an Excel sheet with two columns, `oldname` and `newname`, you can use the `data.table` package to rename columns by referencing the old name. More on that here: https://stackoverflow.com/questions/20987295/rename-multiple-columns-by-names – Matt Nov 18 '20 at 01:44
  • I actually don't want to rename the variables, I want to add the labels to them so that they show up under the column, and so I can use the labels when creating freq tables, etc. Just wondered if there was an easier way than typing out each variable if they're already in the format X1...X20 (etc). – Pre Nov 18 '20 at 01:48