基于另一列(日期)的顺序编号 R

Sequential numbering based on another column (date) R

我需要根据日期创建一个带有序号的列。同一日期有多行,应如下所示:

       nest in.temp out.temp age  date
1 501 (913)    18.0     11.5  0 06/02
2 501 (913)    17.5     12.0  0 06/02
3 501 (913)    17.5     12.0  0 06/02
4 501 (913)    17.5     12.5  0 06/02
5 501 (913)    17.5     14.0  1 06/03
6 501 (913)    18.0     13.0  1 06/03

但是,它正在输出 NA 警告。我正在使用的代码适用于包含多个需要相同输出的文件的文件夹,因此如果代码在循环中工作会很有帮助。但是,我将合并数据框,这样也可以在事后完成。

nestlist1 <- lapply(1:length(nestlist), function(z) {
  #creates nests in loop
  k <- nestlist[[z]]
  k <- k[!is.na(k$In), ]
  #separates time and date from one another
  k$time <- c(format(as.POSIXct(strptime(k$DateTime, "%Y-%m-%d %H:%M:%S", tz="")),
                     format="%H:%M"))
  k$date <- c(format(as.POSIXct(strptime(k$DateTime, "%Y-%m-%d %H:%M", tz="")),
                     format="%m/%d"))
  k$time <- strptime(k$time, "%H:%M")
  #sets parameters for temperature being observed
  a <- lapply(unique(k$date), function(i)
    d <- k[k$date == i & k$time >= "2021-01-14 03:00:00 MDT" & 
             k$time <=  "2021-01-14 06:00:00 MDT", ])
  #names based on the date
  names(a) <- gsub( ".xlsx", "", unique(k$date))
  #number of variables at each date
  rn <- lapply(a, nrow)
  #????
  a <- a[rn > 0]
  b <- unlist(lapply(a, function(x) {
    x$In
  }))
  d <- unlist(lapply(a, function(x) {
    x$Out
  }))
  c <- unlist(lapply(a, function(x) {
    x$date
  }))
  e <- unlist(lapply(a, function(x) {
    x$date
  }))
  e <- as.data.frame(e)
  c <- as.data.frame(c)
  b <- as.data.frame(b)
  d <- as.data.frame(d)
  b <- cbind(b, d)
  b <- cbind(b, c)
  b <- cbind(b, e)
  colnames(b)[1] <- 'in.temp' 
  colnames(b)[2] <- 'out.temp'
  colnames(b)[3] <- 'day'
  colnames(b)[4] <- 'date'
  is.num <- sapply(b, is.numeric)
  b[is.num] <- lapply(b[is.num], round, 1)
  b$day <- as.numeric(b$day)
  head(b)
  xx <- data.frame(nest=names(nestlist)[z], in.temp= b$in.temp, 
                   out.temp=b$out.temp, age=b$day, date=b$date)
  return(xx)
})

使用 tidyverselubridate:

library(tidyverse)
library(lubridate)

read_delim('example.txt', delim = ' ') %>% 
  mutate(
    date = parse_date_time(date, 'md'),
    age = day(date) - day(date)[1L] 
  )

parse_date_time 采用 06/02 形式的日期并将其转换为 0000-06-02。 lubridate 中的 day 函数然后从 date 中提取日期,并且 - day(date)[1L] 部分归一化到第一行,导致 0, 0, 0, 0, 1, 1.


我用这个作为输入 example.txt:

nest in.temp out.temp date
501 18.0 11.5 06/02
501 17.5 12.0 06/02
501 17.5 12.0 06/02
501 17.5 12.5 06/02
501 17.5 14.0 06/03
501 18.0 13.0 06/03