Search code examples
rdata.tablemeanr-colnames

How to calculate mean of only few columns of a text file in R?


I am using following code in R to calculate mean of all columns in various text files. Now, I need to modify the existing code to calculate mean of only few columns e.g. Temp [C], Press [Pa], Pow [W] etc. (All the columns in the txt file are in float numbers, the first column is Date/Time).

Could anyone please help in modifying the existing code to achieve the desired objective.

xyz <- xy %>% 
    as_tibble() %>%
    group_by(time_sp = lubridate::floor_date(`Date/Time`, "15 mins")) %>% 
    summarise(across(where(is.numeric), ~ if(mean(is.na(.x)) > 0.5) NA else mean(.x, na.rm = 
TRUE)))

  write.csv(xyz, paste0(dirlist[idx],"15row.csv"), row.names = FALSE)

Sample input data is as follows:

Date/Time Temp [C] TQ [C] Press [Pa] PQ [Pa] Pow [W] PowQ [W] ...
1990-02-01S0:00:01 27 5 298 -4 1278 1... 
1990-02-01S0:00:02 25 3 298 -4 1277 0...
....
...

Solution

  • cn <- c('Temp [C]', 'Press [Pa]', 'Pow [W]')
    xyz <- xy[ , cn ] %>% 
        as_tibble() %>%
        group_by(time_sp = lubridate::floor_date(`Date/Time`, "15 mins")) %>% 
        summarise(across(where(is.numeric), ~ if(mean(is.na(.x)) > 0.5) NA else mean(.x, na.rm = 
    TRUE)))
    
      write.csv(xyz, paste0(dirlist[idx],"15row.csv"), row.names = FALSE)