R 中日期的 else 语句
If else statements for dates in R
您好,我正在尝试在我的数据框中创建一个新列来表示 "if 'SightDate' is between 7-15 and 2-15 return TRUE 1 else FALSE [0]",但似乎无法在 r 中找到日期函数的语法。这是我到目前为止所拥有的。
#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)
我收到这个错误
Error in charToDate(x) :
character string is not in a standard unambiguous format
我不确定为什么,因为当我去掉逗号时,我得到的是 "unexpected numeral 1"。
此外,我正在尝试使用我在论坛上找到的函数为我的数据分配季节。但我正在为 R 中的日期分配而苦苦挣扎,似乎无法理解它。我希望能够将季节分配包含在标题为 "SightSeason" 或类似名称的新列中。
sightingsData$sightSeason <- getSeason(sightingsData$SightMonthDay)
但似乎看不出我将在下面的函数中解释它的位置......在我将我的 sightingsData$SightData 转换为 2016 年值之前或之后或之后。这让我犹豫不决,我想知道我是否应该创建一个中间列,我可以在其中存储我的 sightingsData$SightDate 到 2016 年值的转换——类似于 sightingsData$2016Sight 这样我就不会永久改变我的 df。这个我不是很清楚。对于漫无边际,我深表歉意,但我不确定如何澄清我的问题。任何人,这是 "get seasons function":
### 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")))
任何关于我如何应用该函数的见解,或者如果您知道将季节分配给我的数据集的不同方法,将不胜感激!澄清一下,我使用 2016 年是因为它是最后一个有完整冬至和春分数据的闰年。
这是我数据的 20 个观察随机样本:
SightDate SightMonthDay
17092 10/23/2017 10-23
129 3/13/2009 03-13
20671 1/17/2018 01-17
以及 dput(SampleData) 的输出
> 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")
抱歉,我不太清楚如何 post 我的数据样本供其他人操作。
感谢您的宝贵时间!
您不需要 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
您好,我正在尝试在我的数据框中创建一个新列来表示 "if 'SightDate' is between 7-15 and 2-15 return TRUE 1 else FALSE [0]",但似乎无法在 r 中找到日期函数的语法。这是我到目前为止所拥有的。
#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)
我收到这个错误
Error in charToDate(x) :
character string is not in a standard unambiguous format
我不确定为什么,因为当我去掉逗号时,我得到的是 "unexpected numeral 1"。
此外,我正在尝试使用我在论坛上找到的函数为我的数据分配季节。但我正在为 R 中的日期分配而苦苦挣扎,似乎无法理解它。我希望能够将季节分配包含在标题为 "SightSeason" 或类似名称的新列中。
sightingsData$sightSeason <- getSeason(sightingsData$SightMonthDay)
但似乎看不出我将在下面的函数中解释它的位置......在我将我的 sightingsData$SightData 转换为 2016 年值之前或之后或之后。这让我犹豫不决,我想知道我是否应该创建一个中间列,我可以在其中存储我的 sightingsData$SightDate 到 2016 年值的转换——类似于 sightingsData$2016Sight 这样我就不会永久改变我的 df。这个我不是很清楚。对于漫无边际,我深表歉意,但我不确定如何澄清我的问题。任何人,这是 "get seasons function":
### 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")))
任何关于我如何应用该函数的见解,或者如果您知道将季节分配给我的数据集的不同方法,将不胜感激!澄清一下,我使用 2016 年是因为它是最后一个有完整冬至和春分数据的闰年。
这是我数据的 20 个观察随机样本:
SightDate SightMonthDay
17092 10/23/2017 10-23
129 3/13/2009 03-13
20671 1/17/2018 01-17
以及 dput(SampleData) 的输出
> 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")
抱歉,我不太清楚如何 post 我的数据样本供其他人操作。
感谢您的宝贵时间!
您不需要 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