Posts

Showing posts with the label r

R: How to find the first non-zero element in a datframe by group

I have the following dataframe I want to subset this dataset such that I will only have rows between the first record and the the first 1 value in the flag column for each group and if there is no 1, that group should not appear at all. Something like this: I saw some answers at Dplyr : how to find the first-non missing string by groups? But it is for non-missing and I have both non-missing and 0 values. Or in dplyr (same result) Data used: Benchmark Output: Benchmark code: Using dplyr::slice , then the equivalent base R using by . And finally one just for performance, with benchmark. All robust for the case with no Flag==1 in a group. dplyr base base fast benchmark I did the benchmark on @Lebatsnok modified input, that I remodified because the NAs were not properly recognized as such. MKR's and WWW's solutions are not robust for this case, but i left them in the benchmark anyway. data benchmark code A cumsum based solution using dplyr can be as: Data: A solution using dply...

How to split the quantile results in 5 different data frames?

I'm running the quantile function in a dataframe and when I get the result of the quantile, I'd like to get those results in independent data frames to work with them separately, for example to add or subtract some of the quantile results to other values,etc. Here a very simple example: How Can I get the results in different data frames? What I'd like to get is to subdivide quantile results in 5 independent results (data frames) For example: you are looking for: Now you can write Q1 and it will show up. By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Can I use mutate() to mimic a value i would join from a summarize() with dplyr?

I feel like there is a more elegant way with dplyr to recreate the following result of joining the results of a summarize call with mutate. When I feel there may be a way to use mutate in this way... No need for the inner_join You can just do group_by() with a mutate() . By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Process rows of R dataframe without loop in memory efficient way

The structure of my dataframe data1 , which has over 1.5 million rows, is like this: I need to insert a column Exit.time using values in columns WEEK and END and a cutoff value, which is 1287. Exit.time should have 0 or 1 value based on the following logic: if WEEK = 1287, then Exit.time = 0. if Week not equal to 1287, but WEEK = END then Exit.time = 1, otherwise Exit.time = 0. For this I tried the following for loop and it does what is required in the above dummy data set. The problem is that when I use the above loop in my real data set, even after an hour I am not getting an output. I guess looping is not efficient given the size of the dataset. Is there an alternative way to do what I want? I prefer to maintain the order of rows in data1 since I need to do some merge operations later on. Since you need Exit.time to be 1 when (WEEK == END) & WEEK != 1287 and 0 otherwise, you can use as.numeric on the results of (WEEK == END) & WEEK != 1287 , which changes TRUE...