plyr
Why are my dplyr group_by & summarize not working properly? (name-collision with plyr)
I believe you’ve loaded plyr after dplyr, which is why you are getting an overall summary instead of a grouped summary. This is what happens with plyr loaded last. library(dplyr) library(plyr) df %>% group_by(DRUG,FED) %>% summarize(mean=mean(AUC0t, na.rm=TRUE), low = CI90lo(AUC0t), high= CI90hi(AUC0t), min=min(AUC0t, na.rm=TRUE), max=max(AUC0t,na.rm=TRUE), sd= sd(AUC0t, na.rm=TRUE)) mean low high min max sd 1 … Read more
How to create a lag variable within each group?
You could do this within data.table library(data.table) data[, lag.value:=c(NA, value[-.N]), by=groups] data # time groups value lag.value #1: 1 a 0.02779005 NA #2: 2 a 0.88029938 0.02779005 #3: 3 a -1.69514201 0.88029938 #4: 1 b -1.27560288 NA #5: 2 b -0.65976434 -1.27560288 #6: 3 b -1.37804943 -0.65976434 #7: 4 b 0.12041778 -1.37804943 For multiple columns: … Read more
What does the dot mean in R – personal preference, naming convention or more?
A dot in function name can mean any of the following: nothing at all a separator between method and class in S3 methods to hide the function name Possible meanings 1. Nothing at all The dot in data.frame doesn’t separate data from frame, other than visually. 2. Separation of methods and classes in S3 methods … Read more
dplyr: “Error in n(): function should not be called directly”
I presume you have dplyr and plyr loaded in the same session. dplyr is not plyr. ddply is not a function in the dplyr package. Both dplyr and plyr have the functions summarise/summarize. Look at the results of conflicts() to see masked objects.
dplyr summarise: Equivalent of “.drop=FALSE” to keep groups with zero length in output
The issue is still open, but in the meantime, especially since your data are already factored, you can use complete from “tidyr” to get what you might be looking for: library(tidyr) df %>% group_by(b) %>% summarise(count_a=length(a)) %>% complete(b) # Source: local data frame [3 x 2] # # b count_a # (fctr) (int) # 1 … Read more
Count number of rows by group using dplyr
There’s a special function n() in dplyr to count rows (potentially within groups): library(dplyr) mtcars %>% group_by(cyl, gear) %>% summarise(n = n()) #Source: local data frame [8 x 3] #Groups: cyl [?] # # cyl gear n # (dbl) (dbl) (int) #1 4 3 1 #2 4 4 8 #3 4 5 2 #4 6 … Read more
Applying a function to every row of a table using dplyr?
As of dplyr 0.2 (I think) rowwise() is implemented, so the answer to this problem becomes: iris %>% rowwise() %>% mutate(Max.Len= max(Sepal.Length,Petal.Length)) Non rowwise alternative Five years (!) later this answer still gets a lot of traffic. Since it was given, rowwise is increasingly not recommended, although lots of people seem to find it intuitive. … Read more
Change value of variable with dplyr
We can use replace to change the values in ‘mpg’ to NA that corresponds to cyl==4. mtcars %>% mutate(mpg=replace(mpg, cyl==4, NA)) %>% as.data.frame()