[R] apply function within different groups
Hi, I have a very big data frame and I would like to apply a function to one of the columns within different groups and obtain another dataframe My data frame is like this: group var1 var2 myvar group1 1 a 100 group2 2 b 200 group2 34 c 300 group3 5 d 400 group3 6 e 500 group4 7 f 600 and I woud like to apply this function to column myvar: mifunc = function(vec) { vec=as.vector(vec) for (i in 1:(length(vec)-1)){ vec[i]=vec[i+1]-1 } return(vec) } by the groups in column group. I would like to obtain the same dataframe but with f(myvar) instead of myvar. How can I do this? Thanks, Estefania [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] apply function within different groups
Thanks a lot!!! It works perkectly! Just one thing, is there a way to preserve my original data frame so i don´t need to join both tables? I could do it with rbind but my original data frame is not in order, so Thanks again! From: arun smartpink...@yahoo.com To: Estefanía Gómez Galimberti tef...@yahoo.com Cc: R help r-help@r-project.org Sent: Thursday, May 23, 2013 12:48 PM Subject: Re: [R] apply function within different groups Hi, May be this helps: dat1- read.table(text= group var1 var2 myvar group1 1 a 100 group2 2 b 200 group2 34 c 300 group3 5 d 400 group3 6 e 500 group4 7 f 600 ,sep=,header=TRUE,stringsAsFactors=FALSE) library(plyr) ddply(dat1,.(group),summarize, f_myvar=mifunc(myvar)) # group f_myvar #1 group1 NA #2 group2 299 #3 group2 300 #4 group3 499 #5 group3 500 #6 group4 NA A.K. - Original Message - From: Estefanía Gómez Galimberti tef...@yahoo.com To: r help help r-help@r-project.org Cc: Sent: Thursday, May 23, 2013 11:30 AM Subject: [R] apply function within different groups Hi, I have a very big data frame and I would like to apply a function to one of the columns within different groups and obtain another dataframe My data frame is like this: group var1 var2 myvar group1 1 a 100 group2 2 b 200 group2 34 c 300 group3 5 d 400 group3 6 e 500 group4 7 f 600 and I woud like to apply this function to column myvar: mifunc = function(vec) { vec=as.vector(vec) for (i in 1:(length(vec)-1)){ vec[i]=vec[i+1]-1 } return(vec) } by the groups in column group. I would like to obtain the same dataframe but with f(myvar) instead of myvar. How can I do this? Thanks, Estefania [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] remove from column a group of elements I have in another vector
Thank you Sarah!! My data frame is b and I have to remove from column anslogin every value from vector quit b= ht dispsplit anslogin month_ 1038 162 4627 1 475 1 4305 1 205 103 4136 1 296 1 4627 1 784 9 51274 1 451 75 4593 1 quit= 4079 4081 4095 4096 4119 4134 So, i should do b[!(b$ansologin%in%quit),], right? How does the %in% work??? Is it a normal in and the not comes with the ! ate the beggining? I have never used it, so thanks for teaching me this!. Thanks again, Estefania From: Sarah Goslee sarah.gos...@gmail.com To: Estefanía Gómez Galimberti tef...@yahoo.com Cc: r-help@r-project.org r-help@r-project.org Sent: Friday, December 21, 2012 4:56 PM Subject: Re: [R] remove from column a group of elements I have in another vector You can probably do it with not in in R too: for a data frame x where you want to remove rows where values in column A are not in the vector y: x[!(x$A %in% y), ] If you'd provided a reproducible example, I could give code that works in your particular circumstance. Sarah On Fri, Dec 21, 2012 at 12:43 PM, Estefanía Gómez Galimberti tef...@yahoo.com wrote: Hi, I have a data frame and I would need to remove from one of the columns a group of elements I have in another vector. How can I do that? I know how to do it with criteria but i would need to do it in a more automatic way In SQL I would use where not in Thank you, Estefania -- Sarah Goslee http://www.functionaldiversity.org [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] remove from column a group of elements I have in another vector
Hi, I have a data frame and I would need to remove from one of the columns a group of elements I have in another vector. How can I do that? I know how to do it with criteria but i would need to do it in a more automatic way In SQL I would use where not in Thank you, Estefania [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.