I am looking to take the average of certain columns, and then renaming the first 2 columns to make a brand new data frame.
Lets say this is DF
Participant | Date | Score1 | Place | Job |
---|---|---|---|---|
First | 10_Sep_Mon | 12 | 3 | 5.5 |
Second | 10_Sep_Mon | 11 | 2 | 5.7 |
Third | 10_Sep_Mon | 14 | 2.5 | 5 |
Fourth | 10_Sep_Mon | 21 | 2.8 | 5.1 |
I want to make a new data frame that looks like this, where the first column name is named different, the date is kept the same, and all columns starting the 3rd and until the very end ncol(DF), cause there are lots of columns than 3.
This would be a new data frame called DF2
Participant | Date | Score1 | Place | Job |
---|---|---|---|---|
Averaged | 10_Sep_Mon | 14.5 | 2.6 | 5.3 |
What is the best way to apply this calculation?
Here is some code if that may help. Thank you!
Participant <- c("First", "Second", "Third", "Fourth")
Date <- c("10_Sep_Mon", "10_Sep_Mon", "10_Sep_Mon", "10_Sep_Mon")
Score1 <- c(12, 11, 14, 21)
Place <- c(3, 2, 2.5, 2.8)
Job <- c(5.5, 5.7, 5, 5.1)
DF <- data.frame(Participant, Date, Score1, Place, Job)
CodePudding user response:
cbind
first row, first two columns to the colMeans
of the remaining columns (need to be t
ransposed).
cbind(DF[1, 1:2], t(colMeans(DF[-(1:2)])))
# Participant Date Score1 Place Job
# 1 First 10_Sep_Mon 14.5 2.575 5.325
CodePudding user response:
Or using dplyr
you could do:
library(dplyr, warn = FALSE)
DF %>%
group_by(Participant = "Averaged", Date) %>%
summarise(across(everything(), mean), .groups = "drop")
#> # A tibble: 1 × 5
#> Participant Date Score1 Place Job
#> <chr> <chr> <dbl> <dbl> <dbl>
#> 1 Averaged 10_Sep_Mon 14.5 2.58 5.32