subset - Serial Subsetting in R -
i working large datasets. have extract values 1 datasets, identifiers values stored in dataset. subsetting twice each value of 1 category. multiple category, have combine such double-subsetted values. doing similar shown below, think there must better way it.
example datasets
set.seed(1) df <- data.frame(number= seq(5020, 5035, 1), value =rnorm(16, 20, 5), type = rep(c("food", "bar", "sleep", "gym"), each = 4)) df2 <- data.frame(number= seq(5020, 5035, 1), type = rep(letters[1:4], 4))
extract value grade a
asub_df2 <-subset(df2, type == "a" ) asub_df <-subset(df, number == asub_df2$number) new_a <- cbind(asub_df, grade = rep(c("a"),nrow(asub_df)))
similarly extract value grade b in new_b
, combine analysis.
can use
you can split
'df2' , use lapply
filter(negate(is.null), lapply(split(df2, df2$type), function(x) { x1 <- subset(df, number==x$number) if(nrow(x1)>0) { transform(x1, grade=x$type[1]) } }))
Comments
Post a Comment