Home > Software engineering >  Loop don't go throught the dataframe (R)
Loop don't go throught the dataframe (R)

Time:03-04

I'm trying to convert all the column of my dataframe to the date format, their class is currently chr.

My code is working without the loop (when I specify the column: df$colname)

df_2 <- data.frame(matrix(nrow = nrow(df)))

for (i in colnames(df)){
  var <- data.frame(as.POSIXct(df$i, format ="%Y-%m-%d %H:%M:%S"))
  df_2 <- cbind(df_2, var)
}

My dataframe (df):

col1 col2
2015-01-02 09:43:45 2015-01-05 10:08:48
2015-01-02 10:15:44 2015-01-05 10:21:05
col1<- c("2015-01-02 09:43:45   ", "2015-01-02 10:15:44")
col2 <- c("2015-01-05 10:08:48","2015-01-05 10:21:05")

df <- data.frame(col1, col2)

CodePudding user response:

You can do it without using loops at all, if you want to. Here's a tidyverse solution:

library(tidyverse)

d <- tibble(
       col1=c("2015-01-02 09:43:45", "2015-01-05 10:08:48"),
       col2=c("2015-01-02 10:15:44", "2015-01-05 10:21:05")
     )

d %>% mutate(across(everything(), ~as.POSIXct(., format ="%Y-%m-%d %H:%M:%S")))
# A tibble: 2 × 2
  col1                col2               
  <dttm>              <dttm>             
1 2015-01-02 09:43:45 2015-01-02 10:15:44
2 2015-01-05 10:08:48 2015-01-05 10:21:05

I am unable to reproduce OP's issue with my solution. I've provided the output I obtain above.

It might possibly be a version issue. The versions of the packages I am using are given below:

> packageVersion("tidyverse")
[1] ‘1.3.1’
> packageVersion("tibble")
[1] ‘3.1.5’
> packageVersion("dplyr")
[1] ‘1.0.7’
> packageVersion("base")
[1] ‘4.1.0’

Beyond that, I have no suggestions.

Also, please bear in mind that it is not good practice to upload code, results or data as images for these reasons.

CodePudding user response:

Lets create df2 with the same structure of df:

df_2 <- data.frame(matrix(nrow=nrow(df),ncol=ncol(df)))

Then, the loop should run in a sequence. In your case is the number of columns. Then you want to add the formatted data as date, to df2, according with the number of columns. So, the loop should work like this:

for (i in 1:ncol(df)){
  df_2[i]<-as.POSIXct(df[,i],format ="%Y-%m-%d %H:%M:%S")
  colnames(df_2)<-names(df)
}
  • Related