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

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

How to select the rows with maximum values in each group with dplyr? [duplicate]

Try this: result <- df %>% group_by(A, B) %>% filter(value == max(value)) %>% arrange(A,B,C) Seems to work: identical( as.data.frame(result), ddply(df, .(A, B), function(x) x[which.max(x$value),]) ) #[1] TRUE As pointed out in the comments, slice may be preferred here as per @RoyalITS’ answer below if you strictly only want 1 row per group. This answer will … Read more

Why does summarize or mutate not work with group_by when I load `plyr` after `dplyr`?

The problem here is that you are loading dplyr first and then plyr, so plyr’s function summarise is masking dplyr’s function summarise. When that happens you get this warning: library(plyr) Loading required package: plyr —————————————————————————————— You have loaded plyr after dplyr – this is likely to cause problems. If you need functions from both plyr … Read more

Reshape three column data frame to matrix (“long” to “wide” format) [duplicate]

There are many ways to do this. This answer starts with what is quickly becoming the standard method, but also includes older methods and various other methods from answers to similar questions scattered around this site. tmp <- data.frame(x=gl(2,3, labels=letters[24:25]), y=gl(3,1,6, labels=letters[1:3]), z=c(1,2,3,3,3,2)) Using the tidyverse: The new cool new way to do this is … Read more