繁体   English   中英

从 R 中 3 层数据框中时间列中的日期中删除时间

[英]Removing time from dates in time column in 3 layer data frame in R

我正在尝试从 R 中我的数据框的 3 层中的每一层的时间列中删除“T00:00:00”。我不确定如何执行此操作。 我的数据如下所示:

[[1]]
# A tibble: 1 x 3

  observation HRpcode timeseries

        <int> <chr>   <chr>  

1           1 NA      NA    


[[2]]

# A tibble: 74 x 3

   observation time                ` NDVI`

         <int> <chr>               <chr>

 1           1 2014-01-01T00:00:00 0.3793765496776215 

 2           2 2014-02-01T00:00:00 0.21686891782421552

 3           3 2014-03-01T00:00:00 0.3785652933528299 


# ... with 64 more rows


[[3]]

# A tibble: 74 x 3

   observation time                ` NDVI`    

         <int> <chr>               <chr>    

 1           1 2014-01-01T00:00:00 0.4071076986818826

 2           2 2014-02-01T00:00:00 0.09090719657570319

 3           3 2014-03-01T00:00:00 0.35214166081795284


# ... with 64 more rows


[[4]]

# A tibble: 74 x 3

   observation time                ` NDVI` 

         <int> <chr>               <chr>     

 1           1 2014-01-01T00:00:00 0.3412131556625801 

 2           2 2014-02-01T00:00:00 0.18815996897460135

 3           3 2014-03-01T00:00:00 0.5218904976415136 


# ... with 64 more rows

我如何隔离列并将它们相乘?

编辑:这是数据示例。 我使用 dput(head(data)) 来获取数据样本,但该命令不会产生我想要的 6 行样本。 因此,我删除了一些数据,以便我可以将其发布到堆栈交换中。 感谢您的帮助。

list(Files = structure(list(observation = 1L, HRpcode = NA_character_, 
    timeseries = NA_character_), row.names = c(NA, -1L), class = c("tbl_df", 
"tbl", "data.frame")), Ra = structure(list(observation = 1:6, 
    time = c("2014-01-01T00:00:00", "2014-02-01T00:00:00", "2014-03-01T00:00:00", 
    "2014-04-01T00:00:00", "2014-05-01T00:00:00", "2014-06-01T00:00:00"
    ), ` NDVI` = c("0.3793765496776215", "0.21686891782421552", 
    "0.3785652933528299", "0.41027240624704164", "0.4035578030242673", 
    "0.341299793064468")), row.names = c(NA, -6L), class = c("tbl_df", 
"tbl", "data.frame")), Ba = structure(list(observation = 1:6, 
    time = c("2014-01-01T00:00:00", "2014-02-01T00:00:00", "2014-03-01T00:00:00", 
    "2014-04-01T00:00:00", "2014-05-01T00:00:00", "2014-06-01T00:00:00"
    ), ` NDVI` = c("0.4071076986818826", "0.09090719657570319", 
    "0.35214166081795284", "0.4444311032927228", "0.5220702877666005", 
    "0.5732370503295022")), row.names = c(NA, -6L), class = c("tbl_df", 
"tbl", "data.frame")), Tailevu = structure(list(observation = 1:6, 
    time = c("2014-01-01T00:00:00", "2014-02-01T00:00:00", "2014-03-01T00:00:00", 
    "2014-04-01T00:00:00", "2014-05-01T00:00:00", "2014-06-01T00:00:00"
    ), ` NDVI` = c("0.3412131556625801", "0.18815996897460135", 
    "0.5218904976415136", "0.6970128777711452", "0.7229657162729096", 
    "0.535967435470161")), row.names = c(NA, -6L), class = c("tbl_df", 
"tbl", "data.frame")))

您可以使用lapply循环遍历列表并删除"T"之后的所有内容。

假设列表被称为list_df ,我们可以做

list_df <- lapply(list_df, function(x) {
     if("time" %in% names(x)) transform(x, time = sub('T.*', '', x$time))
     else x
})


list_df
#$Files
# A tibble: 1 x 3
#  observation HRpcode timeseries
#        <int> <chr>   <chr>     
#1           1 NA      NA        

#$Ra
#  observation       time              X.NDVI
#1           1 2014-01-01  0.3793765496776215
#2           2 2014-02-01 0.21686891782421552
#3           3 2014-03-01  0.3785652933528299
#4           4 2014-04-01 0.41027240624704164
#5           5 2014-05-01  0.4035578030242673
#6           6 2014-06-01   0.341299793064468
#...
#....

我们可以使用tidyverse方法

library(dplyr)
library(purrr)
library(stringr)
list_df <- map(list_df, ~if('time' %in% colnames(.x)) {  
                         .x %>%
                             mutate(time = str_remove(time, "T.*"))}

                       else .x)

-输出

list_df
#$Files
# A tibble: 1 x 3
#  observation HRpcode timeseries
#        <int> <chr>   <chr>     
#1           1 <NA>    <NA>      

#$Ra
# A tibble: 6 x 3
#  observation time       ` NDVI`            
#        <int> <chr>      <chr>              
#1           1 2014-01-01 0.3793765496776215 
#2           2 2014-02-01 0.21686891782421552
#3           3 2014-03-01 0.3785652933528299 
#4           4 2014-04-01 0.41027240624704164
#5           5 2014-05-01 0.4035578030242673 
#6           6 2014-06-01 0.341299793064468  

#$Ba
# A tibble: 6 x 3
#  observation time       ` NDVI`            
#        <int> <chr>      <chr>              
#1           1 2014-01-01 0.4071076986818826 
#2           2 2014-02-01 0.09090719657570319
#3           3 2014-03-01 0.35214166081795284
#4           4 2014-04-01 0.4444311032927228 
#5           5 2014-05-01 0.5220702877666005 
#6           6 2014-06-01 0.5732370503295022 

#$Tailevu
# A tibble: 6 x 3
#  observation time       ` NDVI`            
#        <int> <chr>      <chr>              
#1           1 2014-01-01 0.3412131556625801 
#2           2 2014-02-01 0.18815996897460135
#3           3 2014-03-01 0.5218904976415136 
#4           4 2014-04-01 0.6970128777711452 
#5           5 2014-05-01 0.7229657162729096 
#6           6 2014-06-01 0.535967435470161  

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM