Home > Enterprise >  Applying group by and summarise data while extracting specific column values
Applying group by and summarise data while extracting specific column values

Time:08-25

I have a large data frame and a subset of the data looks as below.

Sensorvalue <- c(0.5, 0.8, 0.96, 0.87, 0.54, 0.27, 0.94, 0.37, 0.87, 0.58)
SessionID <- c(1, 1, 1, 1, 1, 2, 2, 3, 3, 3)

AnimalID <- c("e-01", "e-01", "e-01", "e-01", "e-01", "e-04", "e-04", "e-07", "e-07", "e-07")

RobotID <- c(104, 104, 104, 104, 104, 101, 101, 108, 108, 108 )

df <- data.frame(Sensorvalue, SessionID, AnimalID, RobotID)

I want to find the Mean and the max sensor values for each sessionID (grouped by session ID) and I used the following code for that.

Summary_df <- df %>% group_by(SessionID) %>% summarise(Mean_Val = Median_Val median(SensorValue), Max_Val = max(SensorValue), Min_Val = min(SensorValue),)

But I want to have the RobotID and the AnimalID related to each sessionID in the Summary_df as well. I tried to use left join, but then duplicate the rows, as there are few rows for each session.

How I supposed to do this in R?

CodePudding user response:

Use unique, assuming that AnimalID and RobotID are indeed unique per SessionID.

Note: you also had several typos and incorrect column names in your example.

 df %>%
  group_by(SessionID) %>%
  summarise(Mean_Val = median(Sensorvalue),
            Max_Val = max(Sensorvalue),
            Min_Val = min(Sensorvalue),
            AnimalID = unique(AnimalID),
            RobotID = unique(RobotID))

# A tibble: 3 x 6
  SessionID Mean_Val Max_Val Min_Val AnimalID RobotID
      <dbl>    <dbl>   <dbl>   <dbl> <chr>      <dbl>
1         1    0.8      0.96    0.5  e-01         104
2         2    0.605    0.94    0.27 e-04         101
3         3    0.580    0.87    0.37 e-07         108

Alternative solution. Full credits for @thelatemail:

df %>%
  group_by(SessionID, AnimalID, RobotID) %>%
  summarise(Mean_Val = median(Sensorvalue),
            Max_Val = max(Sensorvalue),
            Min_Val = min(Sensorvalue)) %>%
  ungroup()
  • Related