kobekko94 / Yusuke-Flexdashboard-Practice

0 stars 0 forks source link

How do I nest sub-columns into a table? #12

Open kobekko94 opened 6 years ago

kobekko94 commented 6 years ago

I have two data frames (day1_sum and day4_sum) that have 2 observations and 18 variables:

## Average score of all questions (day 1)

# dataframe of survey scores for both classes
red <- dt[dt$team == "RED",]
red_day1 <- red[red$day == "day1",grep("adhd",colnames(red))]
red_day1

pink <- dt[dt$team == "PINK",]
pink_day1 <- pink[pink$day == "day1",grep("adhd",colnames(pink))]
pink_day1

# average score of all questions for both teams
red_day1_sum <- red_day1 %>% colSums(na.rm=TRUE)
pink_day1_sum <- pink_day1 %>% colSums(na.rm=TRUE) 

# average score of all questions for both teams
day1_sum <- rbind(red_day1_sum, pink_day1_sum)

## Average score of all questions (day 4)

# dataframe of survey scores for both classes
red <- dt[dt$team == "RED",]
red_day4 <- red[red$day == "day4",grep("adhd",colnames(red))]
red_day4

pink <- dt[dt$team == "PINK",]
pink_day4 <- pink[pink$day == "day4",grep("adhd",colnames(pink))]
pink_day4

# average score of all questions for both teams
red_day4_sum <- red_day4 %>% colSums(na.rm=TRUE)
pink_day4_sum <- pink_day4 %>% colSums(na.rm=TRUE)

# average score of all questions for both teams
day4_sum <- rbind(red_day4_sum, pink_day4_sum)

I want to merge these two data frames such that it displays the same 2 observations and 18 observations, but with 2 sub-observations nested into each observation (does that make sense?) So basically 18 columns, each column with two sub-columns. How do I go about making this?

kobekko94 commented 6 years ago

Also, is it possible to display tables in these issue comments?

davclark commented 6 years ago

Please stop pasting R code into issues! Put this into an actual R or Rmd file and tell me where to look (give a filename). What do you want to do this for? Display? Unfortunately, the underlying data_frame / tibble (and also data.frame) doesn't support nested column names.

I would suggest you separate your task into two steps - getting things into rough shape, and then making things look really nice and clear. Getting the column labels exactly how you want is in step two!

For now, you can start with column names that indicate they are grouped together by making similar names. E.g. adhd1_day1 & adhd1_day4. For now, if things aren't clear, just describe in text before or after the table.