fill()函数在具有两列的R中无效

daupos2t  于 2023-01-18  发布在  其他
关注(0)|答案(1)|浏览(132)

由于某种原因,我似乎不能让fill函数在R中工作。我已经更新了所有的包。
以下是我的数据:

> dput(head(FINALHR_TB_ACC_environment,50))
structure(list(Body_temp = c(14.336, 14.6493333333333, 14.524, 
14.3153333333333, 13.7723333333333, 13.9183333333333, 13.814, 
13.751, 13.229, 13.4796666666667, 13.3543333333333, 13.187, 12.7903333333333, 
12.9366666666667, 12.853, 12.79, 12.5816666666667, 12.602, 12.5606666666667, 
12.54, 12.477, 12.54, 12.54, 12.477, 12.414, 12.414, 12.414, 
12.414, 12.498, 12.456, 12.477, 12.519, 12.7903333333333, 12.623, 
12.707, 12.811, 13.229, 12.999, 13.1036666666667, 13.25, 14.0853333333333, 
13.605, 13.793, 13.981, 15.923, 15.1293333333333, 15.819, 16.2573333333333, 
19.2623333333333, 17.593), Heart_rate = c(NaN, NaN, NaN, NaN, 
NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, 
NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, 
7.56457, 12.08933, NaN, NaN, NaN, NaN, NaN, 9.72277, NaN, NaN, 
NaN, NaN, NaN, NaN, 13.07674, NaN, NaN, 19.44553, NaN, 28.115
), Day = structure(c(16578, 16578, 16578, 16578, 16578, 16578, 
16578, 16578, 16578, 16578, 16578, 16578, 16578, 16578, 16578, 
16578, 16578, 16578, 16578, 16578, 16578, 16578, 16578, 16578, 
16578, 16578, 16578, 16578, 16578, 16578, 16578, 16578, 16578, 
16578, 16578, 16578, 16578, 16578, 16578, 16578, 16578, 16578, 
16578, 16578, 16578, 16578, 16578, 16578, 16578, 16578), class = "Date"), 
    Hour = c(0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 
    3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 
    6L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 10L, 
    10L, 10L, 10L, 11L, 11L, 11L, 11L, 12L, 12L), Minute = c(0, 
    15, 30, 45, 0, 15, 30, 45, 0, 15, 30, 45, 0, 15, 30, 45, 
    0, 15, 30, 45, 0, 15, 30, 45, 0, 15, 30, 45, 0, 15, 30, 45, 
    0, 15, 30, 45, 0, 15, 30, 45, 0, 15, 30, 45, 0, 15, 30, 45, 
    0, 15), Time = structure(c(1432339200, 1432340100, 1432341000, 
    1432341900, 1432342800, 1432343700, 1432344600, 1432345500, 
    1432346400, 1432347300, 1432348200, 1432349100, 1432350000, 
    1432350900, 1432351800, 1432352700, 1432353600, 1432354500, 
    1432355400, 1432356300, 1432357200, 1432358100, 1432359000, 
    1432359900, 1432360800, 1432361700, 1432362600, 1432363500, 
    1432364400, 1432365300, 1432366200, 1432367100, 1432368000, 
    1432368900, 1432369800, 1432370700, 1432371600, 1432372500, 
    1432373400, 1432374300, 1432375200, 1432376100, 1432377000, 
    1432377900, 1432378800, 1432379700, 1432380600, 1432381500, 
    1432382400, 1432383300), tzone = "UTC", class = c("POSIXct", 
    "POSIXt")), Turtle = c("R3L12", "R3L12", "R3L12", "R3L12", 
    "R3L12", "R3L12", "R3L12", "R3L12", "R3L12", "R3L12", "R3L12", 
    "R3L12", "R3L12", "R3L12", "R3L12", "R3L12", "R3L12", "R3L12", 
    "R3L12", "R3L12", "R3L12", "R3L12", "R3L12", "R3L12", "R3L12", 
    "R3L12", "R3L12", "R3L12", "R3L12", "R3L12", "R3L12", "R3L12", 
    "R3L12", "R3L12", "R3L12", "R3L12", "R3L12", "R3L12", "R3L12", 
    "R3L12", "R3L12", "R3L12", "R3L12", "R3L12", "R3L12", "R3L12", 
    "R3L12", "R3L12", "R3L12", "R3L12"), LULC = c(41, 41, 41, 
    41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 
    41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 
    41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 
    41, 41), meanodba = c(NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_), Elevation = c(NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 282.51, NA, 
    NA, NA, 282.76, NA, NA, NA, 281.96, NA, NA, NA, 282.07, NA, 
    NA, NA, 282.07, NA, NA, NA, 281.5, NA), Perc.activ = c(NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 0, NA, NA, 
    NA, 0, NA, NA, NA, 0, NA, NA, NA, 0, NA, NA, NA, 0, NA, NA, 
    NA, 0, NA), Radiation = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 5, 10, 22, 38, 63, 93, 
    118, 130, 159, 162, 197, 244, 252, 282, 379, 401, 501, 579, 
    640, 768, 793, 791, 830, 849, 865, 875, 892), LagRadiation = c(0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 4, 5, 10, 22, 38, 63, 93, 118, 130, 159, 162, 197, 
    244, 252, 282, 379, 401, 501, 579, 640, 768, 793, 791, 830, 
    849, 865, 875), AirTemp = c(10.96, 10.68, 11.31, 11.46, 10.9, 
    10.29, 10.69, 10.35, 10.12, 9.78, 9.55, 9.7, 10.29, 10.76, 
    11.17, 11.78, 11.45, 11.2, 10.75, 10.59, 10.6, 10.88, 11.23, 
    11.66, 12.1, 12.07, 12.14, 12.41, 12.82, 13.19, 13.75, 14.12, 
    14.41, 14.63, 15.07, 15.31, 15.68, 16.16, 16.73, 17.43, 18.43, 
    19.4, 20.11, 20.64, 21.58, 22.26, 22.81, 23.19, 23.32, 23.71
    ), LagAir = c(11.6, 18.81, 10.68, 11.31, 11.46, 10.9, 10.29, 
    10.69, 10.35, 20.27, 9.78, 9.55, 9.7, 15.32, 10.76, 11.17, 
    11.78, 11.45, 11.2, 10.75, 10.59, 10.6, 10.88, 11.23, 11.66, 
    12.1, 12.07, 12.14, 12.41, 12.82, 13.19, 13.75, 14.12, 14.41, 
    14.63, 15.07, 15.31, 15.68, 16.16, 16.73, 17.43, 10.12, 19.4, 
    20.11, 20.64, 21.58, 22.26, 22.81, 23.19, 23.32), Windspeed = c(0, 
    0.55, 1.082, 0.755, 0.154, 0.259, 0.638, 0, 0.34, 0.549, 
    0.668, 1.069, 1.332, 1.307, 1.134, 1.256, 1.192, 0.995, 0.795, 
    0.103, 0.298, 0.388, 1.112, 1.122, 0.297, 0.403, 0.51, 0.403, 
    0.714, 0.439, 0.519, 0.913, 1.13, 1.014, 1.606, 1.458, 1.758, 
    1.768, 1.729, 1.481, 1.345, 1.399, 2.03, 1.776, 2.168, 1.503, 
    2.094, 2.577, 2.552, 2.845), LagWind = c(0, 0, 0.55, 1.082, 
    0.755, 0.154, 0.259, 0.638, 0, 0.34, 0.549, 0.668, 1.069, 
    1.332, 1.307, 1.134, 1.256, 1.192, 0.995, 0.795, 0.103, 0.298, 
    0.388, 1.112, 1.122, 0.297, 0.403, 0.51, 0.403, 0.714, 0.439, 
    0.519, 0.913, 1.13, 1.014, 1.606, 1.458, 1.758, 1.768, 1.729, 
    1.481, 1.345, 1.399, 2.03, 1.776, 2.168, 1.503, 2.094, 2.577, 
    2.552), Precip = c(0, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), Raining = c(FALSE, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA)), class = c("grouped_df", "tbl_df", "tbl", 
"data.frame"), row.names = c(NA, -50L), groups = structure(list(
    Turtle = c("R3L12", "R3L12", "R3L12", "R3L12", "R3L12", "R3L12", 
    "R3L12", "R3L12", "R3L12", "R3L12", "R3L12", "R3L12", "R3L12", 
    "R3L12", "R3L12", "R3L12", "R3L12", "R3L12", "R3L12", "R3L12", 
    "R3L12", "R3L12", "R3L12", "R3L12", "R3L12", "R3L12", "R3L12", 
    "R3L12", "R3L12", "R3L12", "R3L12", "R3L12", "R3L12", "R3L12", 
    "R3L12", "R3L12", "R3L12", "R3L12", "R3L12", "R3L12", "R3L12", 
    "R3L12", "R3L12", "R3L12", "R3L12", "R3L12", "R3L12", "R3L12", 
    "R3L12", "R3L12"), Time = structure(c(1432339200, 1432340100, 
    1432341000, 1432341900, 1432342800, 1432343700, 1432344600, 
    1432345500, 1432346400, 1432347300, 1432348200, 1432349100, 
    1432350000, 1432350900, 1432351800, 1432352700, 1432353600, 
    1432354500, 1432355400, 1432356300, 1432357200, 1432358100, 
    1432359000, 1432359900, 1432360800, 1432361700, 1432362600, 
    1432363500, 1432364400, 1432365300, 1432366200, 1432367100, 
    1432368000, 1432368900, 1432369800, 1432370700, 1432371600, 
    1432372500, 1432373400, 1432374300, 1432375200, 1432376100, 
    1432377000, 1432377900, 1432378800, 1432379700, 1432380600, 
    1432381500, 1432382400, 1432383300), tzone = "UTC", class = c("POSIXct", 
    "POSIXt")), .rows = structure(list(1L, 2L, 3L, 4L, 5L, 6L, 
        7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 
        19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 
        30L, 31L, 32L, 33L, 34L, 35L, 36L, 37L, 38L, 39L, 40L, 
        41L, 42L, 43L, 44L, 45L, 46L, 47L, 48L, 49L, 50L), ptype = integer(0), class = c("vctrs_list_of", 
    "vctrs_vctr", "list"))), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -50L), .drop = TRUE))

下面是我正在尝试的代码:

FINALHR_TB_ACC_environment <- FINALHR_TB_ACC_environment.1 %>%
  group_by(Turtle, Time) %>%
  fill(Precip, .direction = "down") %>%
  fill(Raining, .direction = "down")

基本上,我想填写Precip和Raining列。据我所知,代码是正确的,所以可能它与数据如何存储有关?任何帮助都将不胜感激。

5uzkadbs

5uzkadbs1#

请注意,您在所有行中的时间都不相同,您可能希望改为按日期分组:

df2 <- df%>%
  mutate(Time2=as.Date(Time))%>%
  group_by(Turtle, Time2) %>%
  fill(Precip, .direction = "down") %>%
  fill(Raining, .direction = "down")%>%
  ungroup

head(df2[,c('Precip','Raining')])
# A tibble: 6 x 2
  Precip Raining
   <dbl> <lgl>  
1      0 FALSE  
2      0 FALSE  
3      0 FALSE  
4      0 FALSE  
5      0 FALSE  
6      0 FALSE

相关问题