0

I am attempting to create new data frames for each iteration in R for loop. First, I am calculating percentiles for my data and store them as a data frame.

comparison_data = fread("data.txt",
                            stringsAsFactors=FALSE,
                            col.names=c("c2",
                                         "m1_area", "m1_distance", "m1_tags",
                                         "m2_area", "m2_distance", "m2_tags"
                                         )
                             )

m1_list <- quantile(comparison_data$m1_area, probs = c(.1, .2, .3, .4, .5, .6, .7))
m2_list <- quantile(comparison_data$m2_area, probs = c(.1, .2, .3, .4, .5, .6, .7))
m1_m2_list = as.data.frame(cbind(m1_list, m2_list))

I would like to filter my comparison_data file according to specific percentiles values and save generated data tables.

filter_data_table <- function(a, b) {
  m1_m2_filtered <- dplyr::filter(comparison_data, m1_area > a & m2_area > b)
}

for (i in m1_m2_list) {
  data_frames_list <- sapply(comparison_data, filter_data_table(m1_m2_list$m1_list[i], m1_m2_list$m2_list[i]))
  }

And I get an error:

Error in match.fun(FUN) : 
  'filter_data_table(m1_m2_list$m1_list[i], m1_m2_list$m2_list[i])' is not a function, character or symbol
In addition: Warning messages:
1: In m1_area > a :
  longer object length is not a multiple of shorter object length
2: In m2_area > b :
  longer object length is not a multiple of shorter object length

How can I solve it?

2 Answers 2

1

You can try Map -

m1_m2_list = data.frame(m1_list, m2_list)
result <- Map(filter_data_table, m1_m2_list$m1_list, m1_m2_list$m2_list)

result would have list of dataframes with length same as nrow(m1_m2_list). Each dataframe would be output from one pair of m1_list and m2_list.

Sign up to request clarification or add additional context in comments.

Comments

0

It looks like it's within your sapply function.

Without reproducible data so I can understand what's going on this is my best effort.

Try:

filter_data_table <- function(x =comparison_data, a, b) {
  m1_m2_filtered <- dplyr::filter(x, m1_area > a & m2_area > b)
}

for (i in m1_m2_list) {
  data_frames_list <- sapply(comparison_data, filter_data_table, a = m1_m2_list$m1_list[i], b = m1_m2_list$m2_list[i])
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.