-3

how can I output in my column header the real categorical variable names for each levels. Because it only outputs X1, X2, X3, X4, & X5. Below are my codes to get the buckets and the kable function for the table output.

numerbig <- xtabs(data=lift,formula=Actual~bucket+Llevel)/xtabs(data=lift,formula=weightb~bucket+Llevel)
 numerbig[is.na(numerbig)] <- 0
 denomb <- xtabs(data=lift,formula=Actual~Llevel)/xtabs(data=lift,formula=weightb~Llevel)
 rela2 <- numerbig/rbind(denomb,denomb,denomb,denomb,denomb,denomb,denomb,denomb,denomb,denomb,denomb)-1
 Bucket <- c(1:11)
 df2 = data.frame(cbind(Bucket, rela2))

 kable(df2, results= 'asis', caption= "Lift Relativitiy for Regions", digits=4)

And this is the output of my table. I want to see in the column header the real names of the llevels which are the regions not the x1, x2, x3, x4 & x5. enter image description here

Terru_theTerror
  • 4,918
  • 2
  • 20
  • 39
Bustergun
  • 977
  • 3
  • 11
  • 17
  • Or is there a way that I could put legends for the X1,..X5 variables? – Bustergun Feb 28 '18 at 08:25
  • Hi Bustergun, you need to makes sure to research before you post on StackOverflow. Most of your questions have been answered before. Also, please read about how to ask a good question: there is no need to include most of the code in this example as the dataset i not available for others to use. Have a look at my answer on how to create a demonstration of a minimal example. – Michael Harper Feb 28 '18 at 13:34
  • Also, based on your previous posts, you need to put `results= 'asis'` in the chunk header, not the `kable` function. – Michael Harper Feb 28 '18 at 13:37

1 Answers1

1

This has been answered in lots of previous posts: Changing column names of a data frame

Just use the colnames function to rename columns. Here is a reproducible example:

df <- data.frame(Group = 1:10,
                 X1 = rnorm(10, 1),
                 X2 = rnorm(10, 1),
                 X3 = rnorm(10, 1),
                 X4 = rnorm(10, 1))

colnames(df) <- c("Whatever", "You", "Want", "To", "Use")

kable(df)

enter image description here

In your case, if you want to use the level of the factor, something like this should work:

colnames(df) <- c("Buckets", levels(*the factor variable*))

Check this for further information: https://www.stat.berkeley.edu/classes/s133/factors.html

Michael Harper
  • 14,721
  • 2
  • 60
  • 84