0

enter image description hereI have a bunch of.csv files that share like a dozen different columns, including ones for "DateTime", "Abs Pres", and "Temp", among other things. I've written this code which creates a dataframe only displaying DateTime and Abs Pres but now I want to add the Temp column to that filtered dataframe, while still excluding the other columns. I've tried adding "starts_with(Temp) following the "starts_with(Abs Press) and "starts_with(Date)" but that doesn't work. I feel like I would need to include another if/then statement like the previous columns but I don't need it to actually manipulate the temperature data, I just need it to include that column in the dataframe. I've included a picture of what the working dataframe looks like, but I'd just like it to have one more column labeled "Temp" that matches the correct data point at the correct Date/Time.

HOBOPress <- (lapply(csv_filtered,function(x){
  print(x)
  try({
    d <- read_csv(x, skip=1,local = locale(encoding = "latin1") ) %>% 
      select(starts_with("Date"),starts_with("Abs Pres"), starts_with("Temp")) %>% 
      mutate(filename=x)
    if("Date Time, GMT-04:00" %in% names(d)){
      test <- mdy_hms(d$`Date Time, GMT-04:00`)-60*60
      d$DateTime <- ifelse(is.na(test),mdy_hm(d$`Date Time, GMT-04:00`)-60,test)
    }else {
      test <- mdy_hms(d$`Date Time, GMT-05:00`)
      d$DateTime <- ifelse(is.na(test),mdy_hm(d$`Date Time, GMT-05:00`),test)
    }
    if(any(str_detect(names(d),"psi"))){
      d$AbsPres <- 6.89476 * d %>% select(starts_with("Abs Pres")) %>% pull()
    }else{d$AbsPres <- d %>% select(starts_with("Abs Pres")) %>% pull() %>% 
      as.numeric()}
    d <- d %>% select(filename,DateTime,AbsPres,Temp)
  })
}))
jaynesw
  • 43
  • 7
  • 2
    This code needs to be more concise and reproducible. We cannot see the data that you used, so please include some with `dput(head(df))`. Also, your question will do better if you clearly show your expected output as well, and if you cut down only to the section you need help with. Thanks. – dcsuka Aug 20 '22 at 17:08
  • in your data shown, there is no column named 'Temp' – akrun Aug 20 '22 at 17:47
  • 1
    It's easier to help you if you provide a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. Please [do not post code or data in images](https://meta.stackoverflow.com/q/285551/2372064) – MrFlick Aug 20 '22 at 17:57

0 Answers0