Reduce Dataframe, join rows and add new columns.

I have a DataFrame like:

subject  gender  Weight  Temperature    X    Y     Z      sensor  test   etc.....

101        male       80      23      0.824  0.26 1.20    Head     1
101        male       70      23      0.826  0.24 1.23    Head     1
101        male       81      23      0.829  0.26 1.24    Foot     1
101        male       85      23      0.820  0.23 1.28    Foot     1
101        male       80      23      0.821  0.24 1.24    Head     2
101        male       70      23      0.825  0.24 1.23    Head     2
101        male       81      23      0.829  0.26 1.24    Foot     2
101        male       85      23      0.820  0.23 1.28    Foot     2

so, i want to realize a dataframe where :

  1. I join the rows considering "sensor" and "test" in the columns where is possible ( where there are numeric values) and I create new columns with : mean, variance and standard deviation of this numbers.

Attention: the subject is the same ( "101" ) and the sensor's name are repeated in different tests.
than in final dataset, this operation is iterated on many subject( 101,102,103...).

in this example i would have somethings like:


subject  gender  Weight  Temperature    X(mean)    X(variance)    X(sd)    Y(mean) Y(variance).....    sensor  test   

101        male    75        23          0.825      "xxx"         "xxx"      "xxx"                     Head     1
101        male    83        23          0.819      "xxx"         "xxx"      "xxx"                     Foot     1
101        male    75        23          0.823      "xxx"         "xxx"      "xxx"                     Head     2
   .                         .
   .                         .
   .                         .
   .                    
etc......                ................

Thanks in advance.

I think you want group_by and summarise from dplyr.

Something like

df2 <- df %>%
  group_by(subject, sensor, test) %>%
  summarise(
    X_mean = mean(X),
    X_sd = sd(X), 
    etc...
  )
3 Likes

REALLY REALLY REALLY THANKS:kissing_heart:
It's perfect

1 Like

Yay I answered a question correctly!

2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.