简体   繁体   English

R 中日期的 If else 语句

[英]If else statements for dates in R

Hi I am trying to create a new column in my data frame to say "if 'SightDate' is between 7-15 and 2-15 return TRUE 1 else FALSE [0]" but can't seem to find the syntax for date functions in r.嗨,我正在尝试在我的数据框中创建一个新列来表示“如果 'SightDate' 介于 7-15 和 2-15 之间,则返回 TRUE 1否则 FALSE [0]”,但似乎找不到日期函数的语法在 r 中。 This is what I have so far.这是我到目前为止所拥有的。

#convert SightDate to Month-Day
sightingsData$SightMonthDay <- strptime(as.character(sightingsData$SightDate), "%m/%d/%Y")
sightingsData$SightMonthDay <- format.Date(sightingsData$SightMonthDay, "%m-%d")

#Get whether or not sighting occured during the proposed work period
startWork <- as.Date("07-15", format = "%m-%d")
endWork <- as.Date("02-15", format = "%m-%d")

sightingsData$WorkPeriod = ifelse(sightingsData$SightMonthDay >= startWork & sightingsData$SightMonthDay <= endWork, 1, 0)

I get this error我收到这个错误

Error in charToDate(x) : 
  character string is not in a standard unambiguous format

I'm not sure why, because when I take out the comma, I instead get "unexpected numeral 1".我不知道为什么,因为当我取出逗号时,我得到的是“意外数字 1”。

Additionally, I am trying to assign seasons to my data using a function that I found on a forum.此外,我正在尝试使用我在论坛上找到的 function 为我的数据分配季节。 But I'm struggling with date assignments in R and can't seem to make sense of it.但我在 R 中的日期分配上苦苦挣扎,似乎无法理解。 I want to be able to contain the seasons assignment into a new column titled "SightSeason" or something like that.我希望能够将季节分配包含在标题为“SightSeason”或类似名称的新列中。

sightingsData$sightSeason <- getSeason(sightingsData$SightMonthDay)

But can't seem to see where it is that I would account for that in the function below...before or after I convert my sightingsData$SightData to 2016 values or after.但似乎看不到我将在下面的 function 中解释它的位置……在我将SightingsData$SightData 转换为2016 值之前或之后或之后。 This makes me hesitant, and I wonder if I should create an intermediate column in which I can store the conversion of my sightingsData$SightDate to 2016 values -- something like, sightingsData$2016Sight so that I don't permanently alter my df.这让我犹豫不决,我想知道我是否应该创建一个中间列,我可以在其中存储我的sightingsData$SightDate 到2016 值的转换——比如,singingsData$2016Sight,这样我就不会永久改变我的df。 I'm not really clear on this.我不是很清楚这一点。 I apologize for rambling, but I'm not sure how to clarify my issues.我为漫无目的道歉,但我不确定如何澄清我的问题。 Anywho, this is the "get seasons function": Anywho,这是“获取季节功能”:

### Assign SightMonthDay to Season using 2016 Season Data
getSeason <- function(DATES)
  WS <- as.Date("2016-12-21", format = "%Y-%m-%d") # Winter Solstice
  SE <- as.Date("2016-3-19",  format = "%Y-%m-%d") # Spring Equinox
  SS <- as.Date("2016-6-20",  format = "%Y-%m-%d") # Summer Solstice
  FE <- as.Date("2016-9-22",  format = "%Y-%m-%d") # Fall Equinox

# Convert dates from any year to 2016 dates
d <- as.Date(strftime(DATES, format="2016-%m-%d"))

ifelse (d >= WS | d < SE, "Winter",
        ifelse (d >= SE & d < SS, "Spring",
                ifelse (d >= SS & d < FE, "Summer", "Fall")))

Any insight as to how I can apply that function, or if you know of a different way to assign seasons to my dataset that would be much appreciated, To clarify.关于如何应用 function 的任何见解,或者如果您知道将季节分配给我的数据集的不同方法,将不胜感激,澄清一下。 I'm using 2016 because it was the last leap year for which there is complete solstice and equinox data.我使用 2016 年是因为它是最后一个闰年,有完整的冬至和春分数据。

这是我的 df 的一些截图,它总共有 23585 个观察值。

This is a 20-observation random sample of my data:这是我的数据的 20 次观察随机样本:

       SightDate SightMonthDay
17092 10/23/2017         10-23
129    3/13/2009         03-13
20671  1/17/2018         01-17

And the output of dput(SampleData)和 dput(SampleData) 的 output

> dput(droplevels(SampleData[1:20, ]))
structure(list(SightDate = structure(c(1L, 16L, 18L, 7L, 8L, 
19L, 10L, 6L, 9L, 14L, 13L, 5L, 15L, 2L, 3L, 17L, 4L, 11L, 12L, 
20L), .Label = c("10/13/2015", "10/28/2017", "11/10/2018", "11/2/2018", 
"11/29/2012", "12/14/2017", "12/21/2013", "12/3/2016", "12/5/2017", 
"3/14/2016", "3/22/2015", "3/25/2011", "4/15/2018", "4/4/2014", 
"5/1/2017", "6/26/2016", "8/18/2015", "9/15/2017", "9/18/2015", 
"9/18/2017"), class = "factor"), SightMonthDay = c("10-13", "06-26", 
"09-15", "12-21", "12-03", "09-18", "03-14", "12-14", "12-05", 
"04-04", "04-15", "11-29", "05-01", "10-28", "11-10", "08-18", 
"11-02", "03-22", "03-25", "09-18")), row.names = c(9977L, 11703L, 
15804L, 6177L, 12954L, 9707L, 10774L, 19559L, 18897L, 6546L, 
21349L, 4797L, 14169L, 17403L, 23014L, 9410L, 22758L, 8440L, 
2854L, 15886L), class = "data.frame")

Apologies, I'm not totally clear on how to post a sample of my data for others to manipulate.抱歉,我不太清楚如何发布我的数据样本供其他人操作。

Thank you for your time!感谢您的时间!

You don't need SightMonthDay column.您不需要SightMonthDay列。

#Function to get season
getSeason <- function(d) {
   WS <- as.Date("2016-12-21") # Winter Solstice
   SE <- as.Date("2016-3-19") # Spring Equinox
   SS <- as.Date("2016-6-20") # Summer Solstice
   FE <- as.Date("2016-9-22") # Fall Equinox

 ifelse (d >= WS | d < SE, "Winter",
      ifelse (d >= SE & d < SS, "Spring",
              ifelse (d >= SS & d < FE, "Summer", "Fall")))
}
#Change to standard date format
SampleData$date <- as.Date(SampleData$SightDate, format = '%m/%d/%Y')
#Make date of the same year i.e 2016
SampleData$date <- as.Date(format(SampleData$date, "2016-%m-%d"))
#Get season for each date. 
SampleData$SightSeason <- getSeason(SampleData$date)

head(SampleData)
#       SightDate       date SightSeason
#9977  10/13/2015 2016-10-13        Fall
#11703  6/26/2016 2016-06-26      Summer
#15804  9/15/2017 2016-09-15      Summer
#6177  12/21/2013 2016-12-21      Winter
#12954  12/3/2016 2016-12-03        Fall
#9707   9/18/2015 2016-09-18      Summer

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

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