Home > Net >  How do I work with multiple line headers in R?
How do I work with multiple line headers in R?

Time:01-06

I have the following data frame:

fruit <- c("What is your favorite fruit?", "apple", "grape", "lemon")
color <- c("What is the color of the fruit?", "red", "purple", "yellow")
taste <- c("How does the fruit taste?", "sweet", "sweet", "sour")

df <- data.frame(fruit, color, taste)
View(df)

I want the first two rows to be the headers.

Ideally, I want to be able to add a column called "number" and have it not have a second row header. I want something that looks like this:

df <- df %>%
  mutate(
    number = c(NA, 3, 7, 8)
  )

But I don't want to have to add the NA there. Preferably, I can just do this:

df <- df %>%
  mutate(
    number = c(3, 7, 8)
  )

And get the same df and without getting the Error in `mutate()`:! Problem while computing `number = c(3, 7, 8)`. ✖ `number` must be size 4 or 1, not 3. error.

In Excel, I can just hide the second row. Is there a "hide" option in R?

CodePudding user response:

As r2evens suggested, survey questions are often stored as labels. If your primary objective is how to preserve the data elements, you can also consider using a list. Keep your data in a data frame as one element in the list, and the questions as a vector and another element of the list.

So consider df as your dataframe and question as your vector of questions.


number <- c(3,7,8)
fruit <- c("apple", "grape", "lemon")
color <- c("red", "purple", "yellow")
taste <- c("sweet", "sweet", "sour")

df <- data.frame(number, fruit, color, taste)

question <- c("What is your favorite fruit?", "What is the color of the fruit?", "How does the fruit taste?")

You can then combine them into a named list.

myList <- list(df = df, question = question)

Not very pretty

> myList
$df
  number fruit  color taste
1      3 apple    red sweet
2      7 grape purple sweet
3      8 lemon yellow  sour

$question
[1] "What is your favorite fruit?"    "What is the color of the fruit?" "How does the fruit taste?"   

But when you want to just see the data you can do this.

> myList$df
  number fruit  color taste
1      3 apple    red sweet
2      7 grape purple sweet
3      8 lemon yellow  sour

And in those cases you need the questions, you can call on them. In this case, I replace the field names with the question, but I have to remove the number column first unless I put in a place holder for that column.

myListQ <-myList$df[-1] 
names(myListQ) <- myList$question
myListQ

> myListQ
  What is your favorite fruit? What is the color of the fruit? How does the fruit taste?
1                        apple                             red                     sweet
2                        grape                          purple                     sweet
3                        lemon                          yellow                      sour

CodePudding user response:

Base R solution:

# Rename the data.frame's vectors and add the 
# the number column: result_df => data.frame
result_df <- within(
  setNames(
    df[-1,],
    df[1,]
  ),
  {
    number = c(3, 7, 8)
  }
)

# Print the result to the console: 
# data.frame => stdout(console)
result_df
  • Related