Looping through vector of dates in R drops class info

后端 未结 3 1771
Happy的楠姐
Happy的楠姐 2021-01-23 01:22

Here is my example.

my_df <- data.frame(col_1 = c(1,2), 
col_2 = c(as.Date(\'2018-11-11\'), as.Date(\'2016-01-01\')))
dates_list <- my_df$col_2
for(el in d         


        
3条回答
  •  孤街浪徒
    2021-01-23 02:10

    The cause of the problem could be that dates_list <- my_df$col_2 coerces the column to a date vector:

    dates_list <- my_df$col_2
    class(dates_list)
    > [1] "Date"
    

    so another solution would be to resolve this, as follows:

    dates_list <- my_df["col_2"]
    class(dates_list)
    [1] "data.frame"
    
    for(el in dates_list){
        print(el)
    }
    [1] "2018-11-11" "2016-01-01"
    

提交回复
热议问题