我正在尝试从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")))
答案 0 :(得分:0)
您可以使用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
#...
#....
答案 1 :(得分:0)
我们可以使用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