这可能是一个简单的问题,但我是R的新手,无法找到答案(或者谷歌搜索错误的东西)。 我目前正在开展一个涉及删除少于5分钟的所有时间值的项目。数据的一个示例如下,使用" lubridate"封装
private readonly UserService _userService;
private readonly TechniqService _techniqService;
// global variable
private readonly string _userRole;
public BuildingController(IMapper mapper, UserService userService, TechniqService techniqService)
{
_userService = userService;
_techniqService = techniqService;
// is it ok to define this variable like this?
var userId = GetUserId();// this fetch the userId from a token sent with each request
_userRole = _userService.GetUserRoleFromId(userId);
}
现在我希望删除所有小于5分钟的值。因此,我希望获得的最终数据集是:
Time
19S
1M 24S
7M 53S
11M 6S
.
.
.
任何帮助都会很棒! 谢谢!
答案 0 :(得分:2)
你可以这样做:
df <- df[df$time > ms('5:00'), ]
结果:
> df
time value
3 7M 53S 3
4 11M 6S 4
扼杀,将其转换为dplyr代码;它不起作用:
filter(df, time > ms('5:00'))
结果:
time
1 53S
2 1M 6S
Warning message:
In format.data.frame(x, digits = digits, na.encode = FALSE) :
corrupt data frame: columns will be truncated or padded with NAs
我问了一个问题并找到答案here。你得到了很好的解决方案:
df %>%
mutate(time = as.numeric(time)) %>%
filter(time > as.numeric(ms('5:00'))) %>%
mutate(time = ms(paste0(floor(time/60),':',round((time/60 - floor(time/60))*60))))
数据:
df <- data.frame(time = ms(c('0:19','1:24','7:53','11:6')), value = 1:4)
答案 1 :(得分:0)
试试这个..
{{1}}