Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Taking Average of Certain Columns while keeping certain columns untouched

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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)

>Solution :

cbind first row, first two columns to the colMeans of the remaining columns (need to be transposed).

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading