r在数据框中的某个值以下和之后选择值 [英] r select values below and after a certain value in a dataframe

查看:64
本文介绍了r在数据框中的某个值以下和之后选择值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,如何从表中选择某些值。我有一个包含时间和值的表格,我想在特定时间后获得下面的行。



Example-Data.Frame。

 时间值
02:51 0.08033405
05:30 0.43456738
09:45 0.36052075
14:02 0.45013807
18:55 0.05745870
....#等

时间编码为字符,但可以格式化。
现在,例如,我有时间 6:15,并且想要从表中获取该时间之前和之后的时间值(0.43456738和0.36052075)。
数据库实际上非常庞大,而且我有很多时间值。
任何人都对如何实现此目标有很好的建议?



感谢
Curlew

解决方案

  value_before<-example_df [which(example_df $ time == 09:45)-1,] $ value 
value_after< ;-example_df [which(example_df $ time == 09:45)+ 1,] $ value

#这可能成为函数

return_values<-函数(df,cutoff){
value_before<-df [which(df $ time == cutoff)-1,] $ value
value_after<--df [which(df $ time == cutoff) +1,] $ value
return(list(value(before_value,value_after))
}

return_values(exmaple_df, 09:15)

#大型数据集的解决方案。

  library(data.table)
df<-data.frame(time = 1:1000000,value = rnorm(1000000))
#创建两个偏移量
df $ nvalue<-c(df $ value [2:dim(df)[1]],NA)
df $ pvalue<--c(NA,df $ value [2:dim(df)[1] ])
new_df<-data.table(df)
setkey(new_df, time)

n ew_df [time == 10]
时间值pvalue nvalue
[1,] 10 -0.8488881 -0.1281219 -0.5741059


> new_df [time == 1234]
时间值pvalue nvalue
[1,] 1234 -0.3045015 0.708884 -0.5049194


i have a question how to select certain values from a table. I have a table with times and values and i want to get the row below and after a certain time.

Example-Data.Frame.

Time   Value
02:51  0.08033405 
05:30  0.43456738 
09:45  0.36052075 
14:02  0.45013807 
18:55  0.05745870
....# and so on

Time is coded as character, but can be formatted. Now i have for example the time "6:15" and want to get the values of the time before and after this time from the table (0.43456738 and 0.36052075). The database is in fact quite huge and i have a lot of time values. Anyone has a nice suggestion how to accomplish this?

thanks Curlew

解决方案

value_before <- example_df[which(example_df$time=="09:45")-1, ]$value
value_after <- example_df[which(example_df$time=="09:45")+1, ]$value

# This could become a function

return_values <- function(df,cutoff) {
value_before <- df[which(df$time==cutoff)-1, ]$value
value_after <- df[which(df$time==cutoff)+1, ]$value
return(list(value_before, value_after))
}

return_values(exmaple_df, "09:15")

# A solution for a large dataset.

library(data.table)
df <- data.frame(time = 1:1000000, value = rnorm(1000000))
# create a couple of offsets
df$nvalue <- c(df$value[2:dim(df)[1]],NA)
df$pvalue <- c(NA,df$value[2:dim(df)[1]])
new_df <- data.table(df)
setkey(new_df,"time")

new_df[time==10]
 time      value     pvalue     nvalue
[1,]   10 -0.8488881 -0.1281219 -0.5741059


> new_df[time==1234]
     time      value   pvalue     nvalue
[1,] 1234 -0.3045015 0.708884 -0.5049194

这篇关于r在数据框中的某个值以下和之后选择值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆