'Loop don't go throught the dataframe (R)
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)
Solution 1:[1]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
