Home > Blockchain >  How to use as_hms in a for loop in R
How to use as_hms in a for loop in R

Time:04-24

I have multiple csv files with the columns "timestamp" and "users_holding" I was able to split the timestamp column into two columns "Date" and "Time". I am now trying to round up the Time column for each file. After rounding up I want to recombine the two columns again and remove any double rows. The code for one file is as followed:

    data <- separate(data = data, col = timestamp, into  = c('Date', 'Time'), sep = ' ')
as_hms(data$Time)
data$Time <- round_hms(as_hms(data$Time), 60*60)
data$timestamp <- paste(data$Date,data$Time)
data <- data[!duplicated(data$timestamp), ]

A coded a for loop to separate the columns. However I am not able to build the code for the other steps. My current code is as followed:

    #Separate all timestamps into Date and Time
files <- list.files(pattern = "*csv")
df_list <- lapply(files,read_csv)
df <- bind_rows(df_list)

st_datasets <- df_list
updated_datasets_st <- list()
for (i in 1:length(st_datasets)){
  d <- st_datasets[[i]] %>% separate(col = timestamp, into  = c('Date', 'Time'), sep = ' ')
  updated_datasets_st[[i]] <- d
}
#need help for this part. The code above is working
for (i in 1:length(st_datasets)){
  d <- st_datasets[[i]] %>% round_hms(as_hms(st_datasets$Time), 60*60)
  updated_datasets_st[[i]] <- d
}

CodePudding user response:

Okay, it was a code issue. Sorry. Sigh.

You call the dataset with the list of datasets using an iterator. However when you wrote st_datasets$Time there's no iterator—and it won't work if you add one there.

You can still loop or you can vectorize. I've provided both methods.

Using vectorization

updt = lapply(1:length(st_datasets),
              function(i){
                st_datasets[[i]]$Time <- round_hms(as_hms(st_datasets[[i]]$Time), 60*60)
                st_datasets[[i]]
              })

Using a for loop

upper = list()
for(i in 1:length(st_datasets)){
  st_ds = st_datasets[[i]]
  st_ds$Time = round_hms(as_hms(st_ds$Time), 60*60)
  upper[[i]] <- st_ds
}
upper
  • Related