我目前有棒球数据,希望能够为球员生日前一周和生日之后的比赛过滤数据框。无法按月和日进行过滤。
代码:
#create variable that is the month-day of the start of the 2017 season
start_2017 = format(as.Date(seasons_g_2017[seasons_g_2017$name == 2017,]$starts_on), "%m%d")
#create variable that is the month-day of the end of the 2017 season
end_2017 = format(as.Date(seasons_g_2017[seasons_g_2017$name == 2017,]$ends_on), "%m%d")
#create column in players_data that shows month-day of the player's birthday
players_data$birth_date_filter = as.Date(players_data$birth_date,"%m%d")
#filter players_data to only players who have a birthday during the actual season
players_data = players_data[players_data$birth_date_filter >= start_2017 &
players_data$birth_date_filter <= end_2017,]
答案 0 :(得分:0)
您可以使用lubridate
包的功能来处理日期时间对象。对于您的数据,只需将2017
添加到datatime字符串中,然后将其转换为datatime对象。之后,您可以轻松进行比较和过滤:
图书馆(润滑的)
# simulation
seasons_g_2017 = data.frame(name = 2017,
starts_on = "April 2",
ends_on = "November 1", stringsAsFactors = FALSE)
players_data = data.frame(name = c("John", "Bill"), birth_date = c("January 15", "June 15"), stringsAsFactors = FALSE)
# OP's data
# create variable that is the month-day of the start of the 2017 season
start_2017 <- ymd(paste0(2017, seasons_g_2017[seasons_g_2017$name == 2017, ]$starts_on, sep = " "))
# create variable that is the month-day of the end of the 2017 season
end_2017 <- ymd(paste0(2017, seasons_g_2017[seasons_g_2017$name == 2017, ]$ends_on, sep = " "))
# create column in players_data that shows month-day of the player's birthday
players_data$birth_date_filter <- ymd(paste0(2017, players_data$birth_date, sep = " "))
# filter players_data to only players who have a birthday during the actual season
players_data <- players_data[players_data$birth_date_filter >= start_2017 &
players_data$birth_date_filter <= end_2017,]
players_data
输出:
name birth_date birth_date_filter
2 Bill June 15 2017-06-15