How to sort data in a data frame and then access data in a data frame, by indexing by date?
Tag : r , By : Kilimanjaro
Date : March 29 2020, 07:55 AM
this will help Assuming you have a time series of numbers indexed by unique dates, read.zoo in the zoo package does most of that. Read the 5 vignettes that comes with it as well as ?read.zoo and in particular the zoo-read vignette.
|
Get row indices of data frame A according to multiple matching criteria in that data frame and another data frame, B
Date : March 29 2020, 07:55 AM
I hope this helps you . Let's say we have two data frames in R, df.A and df.B, defined thus: , 1) Using SQL it can readily be done in one statement: library(sqldf)
sqldf('select a.rowid
from "df.B" b
left join "df.A" a on obs_min >= bin_min and obs_max <= bin_max')
rowid
1 4
2 NA
3 5
4 2
5 NA
6 4
7 3
8 4
9 5
10 2
m <- merge(df.B, df.A)
stack(by(m, as.numeric(sub(".*_", "", m$obs_ID)),
with, c(which(obs_min >= bin_min & obs_max <= bin_max), NA)[1]))
values ind
1 4 1
2 NA 2
3 5 3
4 2 4
5 NA 5
6 4 6
7 3 7
8 4 8
9 5 9
10 2 10
sapply(1:nrow(df.B), function(i)
c(which(df.A$bin_max >= df.B$obs_max[i] & df.A$bin_min <= df.B$obs_min[i]), NA)[1])
[1] 4 NA 5 2 NA 4 3 4 5 2
mapply(function(x, y) c(which(x >= df.A$bin_min & y <= df.A$bin_max), NA)[1],
df.B$obs_min,
df.B$obs_max)
seq_len(nrow(df.A)) %*%
(outer(df.A$bin_max, df.B$obs_max, ">=") & outer(df.A$bin_min, df.B$obs_min, "<="))
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 4 0 5 2 0 4 3 4 5 2
|
Want to create new data frames in R by subsetting a data frame in a loop and assign each data frame name based on i valu
Date : March 29 2020, 07:55 AM
hop of those help? Using the same split function as Mark, if it is important for you to save each of the resulting dataframes into your environment, you can use the "list2env" function. df <- data.frame(
month = rep(c("Jan","Mar","Apr"), 4)
, somevalue = runif(12)
)
groups <- unique(df$month)
newdf_list <- split(df, f = df$month)
list2env(setNames(newdf_list, groups), .GlobalEnv)
|
Compare two data.frames to find the rows in data.frame 1 and data.frame 2 which have equal values in selected columns
Date : March 29 2020, 07:55 AM
|
Converting data.frame to data.table, perform calculation, back to data.frame, rest of script malfunctions?
Date : March 29 2020, 07:55 AM
|