Search code examples
rparsingread.tablecolumnsorting

Parsing data in txt file to table (dataframe) in R


I'm very new here and I'm also new in working with R programme. I would like to ask, please, how to parse out the data from a text file to the table in "Environment" section. Specifically, I have no idea of how from the measured temperature data from each Sensor in the file "teplota_19-10-2020" to create columns to make it look the same as in a table in the result picture "tab2" here:

Result table of measured temperatures

Thank you very much for considering of helping me.


Solution

  • In order to create a data frame containing the desired output you need to read the file and do some transformation.
    Aparently "cas - hodiny" translate from Czech into English as "time - hours".

    library(dplyr)
    library(stringr)
    library(purrr)
    
    fl = read.csv2("teplota_19-10-2020.txt", sep = ",")
    names(fl) <- paste0("col", 1:10)
    df <- fl %>% mutate(`hour:min:sec` = str_split(col1, " ") %>% map_chr(.,1),
                    sensor1 = str_split(col1, " ")%>% map_chr(.,3),
                    sensor2 = str_split(col2, " ") %>% map_chr(.,3),
                    sendorOW1 = str_split(col3, " ") %>% map_chr(.,3),
                    sendorOW2 = str_split(col4, " ") %>% map_chr(.,3),
                    setpoint = str_split(col5, " ") %>% map_chr(.,3),
                    ouput = str_split(col6, " ") %>% map_chr(.,3),
                    P = str_split(col7, " ") %>% map_chr(.,3),
                    `I` = str_split(col8, " ") %>% map_chr(.,3),
                    `D` = str_split(col9, " ") %>% map_chr(.,3),
                    ModRizeiTeploty = str_split(col10, " ") %>% map_chr(.,3)
     ) 
    
     df$`cas - hudiny` <- sapply(str_split(df$`hour:min:sec`, ":"), 
                                 function(i) sum(as.numeric(i)/c(1, 60, 3600))
                                 )
     df <- df %>% select("hour:min:sec", `cas - hudiny`, sensor1,  sensor2, sendorOW1, sendorOW2, setpoint, ouput, P, I, D, ModRizeiTeploty) 
    
    

    enter image description here