Re: [R] Extract values from vector and repeat by group
Brilliant - thanks for all the really useful suggestions, problem = solved. Many thanks, Ben Gillespie, Research Postgraduate o---o School of Geography, University of Leeds, Leeds, LS2 9JT o---o Tel: +44(0)113 34 33345 Mob: +44(0)770 868 7641 o---o http://www.geog.leeds.ac.uk/ o-o @RiversBenG o--o From: arun [smartpink...@yahoo.com] Sent: 17 November 2013 16:48 To: R help Cc: Berend Hasselman; Benjamin Gillespie Subject: Re: [R] Extract values from vector and repeat by group Hi, ?merge() sometimes change the order. For example: df1 - df[-12,] df2 - df1 merge(df1, df1[df1$time == 4, c(group, var)], by.x = group, by.y = group, suffixes = c(, GroupSK0)) In that case, df1$ord1 - with(df1,order(group,time)) res - merge(df1, df1[df1$time == 4, c(group, var)], by.x = group, by.y = group, suffixes = c(, GroupSK0)) res[order(res$ord1),-4] #or just library(plyr) join(df2,df2[df2$time==4,c(group,var)],by=group,type=inner) #or you may use: indx - with(df1,ave(time==4,group,FUN=any)) ddply(df1[indx,],.(group),mutate,new=var[time==4]) A.K. On Sunday, November 17, 2013 10:22 AM, Berend Hasselman b...@xs4all.nl wrote: On 17-11-2013, at 15:47, Benjamin Gillespie gy...@leeds.ac.uk wrote: Hi all, I hope you can help. I have a data frame 'df': group=c(rep(1,8),rep(2,10),rep(3,11)) var=rnorm(29) time=c(seq(1,8),seq(1,10),seq(1,11)) df=data.frame(group,var,time) I would like to extract the value from 'var' for each 'group' at 'time'=4 and repeat these extracted values in a new vector ('new') n times where n is the number of rows for each group. I did this by hand as below, but there must be a quicker way: subset=subset(df,df$time==4) subset groupvar time 4 1 0.25312704 12 2 -0.36001284 22 3 0.41947304 df$new=c(rep(0.2531270,8),rep(-0.3600128,10),rep(0.4194730,11)) Any questions please ask, A very similar question was recently asked on Stackoverflow: http://stackoverflow.com/questions/19971763/r-programming-normalizing-a-column-of-data-by-another-entry-in-2-other-columns From the answer given there you could try this set.seed(11) # to make it reproducible group=c(rep(1,8),rep(2,10),rep(3,11)) var=rnorm(29) time=c(seq(1,8),seq(1,10),seq(1,11)) df=data.frame(group,var,time) #df #df[df$time==4, c(group, var)] # merge into original data.frame df - merge(df, df[df$time == 4, c(group, var)], by.x = group, by.y = group, suffixes = c(, GroupSK0)) df Berend __ 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-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] Extract values from vector and repeat by group
Hi all, I hope you can help. I have a data frame 'df': group=c(rep(1,8),rep(2,10),rep(3,11)) var=rnorm(29) time=c(seq(1,8),seq(1,10),seq(1,11)) df=data.frame(group,var,time) I would like to extract the value from 'var' for each 'group' at 'time'=4 and repeat these extracted values in a new vector ('new') n times where n is the number of rows for each group. I did this by hand as below, but there must be a quicker way: subset=subset(df,df$time==4) subset groupvar time 4 1 0.25312704 12 2 -0.36001284 22 3 0.41947304 df$new=c(rep(0.2531270,8),rep(-0.3600128,10),rep(0.4194730,11)) Any questions please ask, Many thanks in advance, Ben Gillespie, Research Postgraduate o---o School of Geography, University of Leeds, Leeds, LS2 9JT o---o Tel: +44(0)113 34 33345 Mob: +44(0)770 868 7641 o---o http://www.geog.leeds.ac.uk/ o-o @RiversBenG o--o __ 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] Splitting times into groups based on a range of times
Hi Arun, Many thanks for that solution; it works well :) I also have some data where times and dates don't follow a 15 min intervals, so, for example: dates=rep(01/02/13,times=20) times=c(12:03:50,12:15:32,12:29:08,12:45:09,13:01:00,13:14:06,13:30:20,13:47:00,13:58:00,14:15:02,14:30:03,14:46:00,15:02:00,15:15:30,15:32:50,15:45:04,16:00:04,16:15:60,16:30:03,16:45:04,17:00:50,17:15:03,17:28:06,17:45:00) x =paste(dates, times) dt=strptime(x, %m/%d/%y %H:%M:%S) dates2=rep(01/02/13,times=8) times2=c(12:00:00,12:30:00,12:45:00,13:15:00,13:30:00,14:00:00,14:45:00,17:30:00) y =paste(dates2, times2) dt2=strptime(y, %m/%d/%y %H:%M:%S) group=c(1,1,2,2,3,3,4,4) df=data.frame(dt2,group) id=c(1,1,1,2,2,2,3,3,3,0,0,4,4,4,4,4,4,4,4,4,4,4,4,0) final.df=data.frame(dt,id) Would you be able to suggest any workarounds for a situation like this? Many thanks in advance, Ben Gillespie, Research Postgraduate o---o School of Geography, University of Leeds, Leeds, LS2 9JT o---o Tel: +44(0)113 34 33345 Mob: +44(0)770 868 7641 o---o http://www.geog.leeds.ac.uk/ o-o @RiversBenG o--o From: arun [smartpink...@yahoo.com] Sent: 11 October 2013 02:29 To: Benjamin Gillespie Cc: R help Subject: Re: [R] Splitting times into groups based on a range of times Hi Ben, I would look into ?findInterval() or ?cut() for an easier solution. indx- match(df[,1],as.POSIXct(dt)) indx2- unique(df[,2]) lst1- lapply(split(indx,((seq_along(indx)-1)%/%2)+1),function(x) seq(x[1], x[2])) res - unlist(lapply(seq_along(lst1),function(i) { val-rep(indx2[i],length(lst1[[i]])) names(val)-lst1[[i]] val })) res1-res[match(seq_along(dt),names(res))] res1[is.na(res1)]- 0 names(res1)- NULL res1 # [1] 1 1 1 2 2 2 3 3 3 0 0 4 4 4 4 4 4 4 4 4 4 4 4 0 identical(id,res1) #[1] TRUE On Thursday, October 10, 2013 8:10 PM, Benjamin Gillespie gy...@leeds.ac.uk wrote: Hi all, I hope you can help with this one! I have a dataframe: 'df' that consists of a vector of times: 'dt2' and a vector of group id's: 'group': dates2=rep(01/02/13,times=8) times2=c(12:00:00,12:30:00,12:45:00,13:15:00,13:30:00,14:00:00,14:45:00,17:30:00) y =paste(dates2, times2) dt2=strptime(y, %m/%d/%y %H:%M:%S) group=c(1,1,2,2,3,3,4,4) df=data.frame(dt2,group) I also have a vector: 'dt' which is a series of times: dates=rep(01/02/13,times=20) times=c(12:00:00,12:15:00,12:30:00,12:45:00,13:00:00,13:15:00,13:30:00,13:45:00,14:00:00,14:15:00,14:30:00,14:45:00,15:00:00,15:15:00,15:30:00,15:45:00,16:00:00,16:15:00,16:30:00,16:45:00,17:00:00,17:15:00,17:30:00,17:45:00) x =paste(dates, times) dt=strptime(x, %m/%d/%y %H:%M:%S) I wish to create a vector which looks like 'id': id=c(1,1,1,2,2,2,3,3,3,0,0,4,4,4,4,4,4,4,4,4,4,4,4,0) The rules I wish to follow to create 'id' are: 1. If a value in 'dt' is either equal to, or, within the range of times within group x in dataframe 'df', then, the value in 'id' will equal x. So, for example, in 'df', group 4 is between the times of 14:45:00 and 17:30:00 on the 01/02/13. Thus, the 12th to 23rd value in 'id' equals 4 as these values correspond to times within 'dt' that are equal to and within the range of 14:45:00 and 17:30:00 on the 01/02/13. If this doesn't make sense, please ask, I'm not sure where to even start with this... possibly the 'cut' function? Many thanks in advance, Ben Gillespie, Research Postgraduate o---o School of Geography, University of Leeds, Leeds, LS2 9JT o---o Tel: +44(0)113 34 33345 Mob: +44(0)770 868 7641 o---o http://www.geog.leeds.ac.uk/ o-o @RiversBenG o--o __ 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-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] Possible loop/ if statement query
Nice, thanks Petr :) Ben Gillespie, Research Postgraduate o---o School of Geography, University of Leeds, Leeds, LS2 9JT o---o Tel: +44(0)113 34 33345 Mob: +44(0)770 868 7641 o---o http://www.geog.leeds.ac.uk/ o-o @RiversBenG o--o From: PIKAL Petr [petr.pi...@precheza.cz] Sent: 11 October 2013 14:06 To: Benjamin Gillespie; r-help@R-project.org Subject: RE: Possible loop/ if statement query Hi not sure if it is the most efficient and clever solution ifelse(b7, NA, cumsum(c(0,diff(!(b7)))==1)) Regards Petr -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-bounces@r- project.org] On Behalf Of Benjamin Gillespie Sent: Thursday, October 10, 2013 12:39 AM To: r-help@R-project.org Subject: [R] Possible loop/ if statement query Dear r genii, I hope you can help. I have vector 'b': b=c((1:10),sort(1:9,decreasing=TRUE),(2:12),sort(6:11,decreasing=TRUE), (7:13)) and, from 'b' I wish to create vector 'c': c=c( NA,NA,NA,NA,NA,NA,1,1,1,1,1,1,1,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA, 2,2,2,2,2,2,2,2,2,2,2,NA,3,3,3,3,3,3,3) The rules I want to use to create 'c' are: A numeric of equal to, or over 7 in 'b' needs to result in a numeric (i.e. not NA) in 'c'; A numeric of less than 7 in 'b' needs to result in NA in 'c'; Where 'groups' of numerics equal to, or over 7 in 'b' are present (i.e. next to each other in the list), the numerics produced in 'c' all need to be the same; Each 'group' of numerics in 'b' must result in a unique numeric in 'c' (and, ideally, they should run in sequence as in 'c' above (1,2,3...). If anyone has any idea where to start on this or can crack it I'll be most grateful!! Many thanks in advance, Ben Gillespie, Research Postgraduate o---o School of Geography, University of Leeds, Leeds, LS2 9JT o- --o Tel: +44(0)113 34 33345 Mob: +44(0)770 868 7641 o---o http://www.geog.leeds.ac.uk/ o-o @RiversBenG o--o __ 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-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] Possible loop/ if statement query
Fantastic - once again, thanks Arun - your knowledge is very impressive! Ben Gillespie, Research Postgraduate o---o School of Geography, University of Leeds, Leeds, LS2 9JT o---o Tel: +44(0)113 34 33345 Mob: +44(0)770 868 7641 o---o http://www.geog.leeds.ac.uk/ o-o @RiversBenG o--o From: arun [smartpink...@yahoo.com] Sent: 10 October 2013 03:24 To: Benjamin Gillespie Cc: R help Subject: Re: [R] Possible loop/ if statement query Hi, Try: b1- b b1[!b1=7]- NA lst1 - split(b1,cumsum(c(0,abs(diff(b=7) indx - as.logical(((seq_along(lst1)-1)%%2)) lst1[indx]- lapply(seq_along(lst1[indx]),function(i) {lst1[indx][[i]]- rep(i,length(lst1[indx][[i]]))}) C2 - unlist(lst1,use.names=FALSE) all.equal(c1,C2) #[1] TRUE A.K. - Original Message - From: arun smartpink...@yahoo.com To: Benjamin Gillespie gy...@leeds.ac.uk Cc: Sent: Wednesday, October 9, 2013 8:19 PM Subject: Re: [R] Possible loop/ if statement query Hi, There should be a simpler way with cumsum(diff()). b=c((1:10),sort(1:9,decreasing=TRUE),(2:12),sort(6:11,decreasing=TRUE),(7:13)) b1- b c1=c( NA,NA,NA,NA,NA,NA,1,1,1,1,1,1,1,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,2,2,2,2,2,2,2,2,2,2,2,NA,3,3,3,3,3,3,3) rl1- rle(b1=7) indx1-(cumsum(rl1$lengths)+1)[!rl1$values] indx2-(cumsum(rl1$lengths))[rl1$values] b1[!b1=7] - NA lst1- split(sort(c(indx1,indx2)),((seq_along(sort(c(indx1,indx2)))-1)%/%2)+1) mat1- sapply(seq_along(lst1),function(i) {x- lst1[[i]]; b1[seq(x[1],x[2])]- i; b1 }) indx2New- !is.na(mat1[,2]) mat1[,2]==2 indx3New- !is.na(mat1[,3]) mat1[,3]==3 mat1[!is.na(mat1[,1]) mat1[,1]3,1] - c(mat1[,2][indx2New],mat1[,3][indx3New]) all.equal(c1,mat1[,1]) #[1] TRUE A.K. - Original Message - From: Benjamin Gillespie gy...@leeds.ac.uk To: r-help@R-project.org r-help@r-project.org Cc: Sent: Wednesday, October 9, 2013 6:39 PM Subject: [R] Possible loop/ if statement query Dear r genii, I hope you can help. I have vector 'b': b=c((1:10),sort(1:9,decreasing=TRUE),(2:12),sort(6:11,decreasing=TRUE),(7:13)) and, from 'b' I wish to create vector 'c': c=c( NA,NA,NA,NA,NA,NA,1,1,1,1,1,1,1,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,2,2,2,2,2,2,2,2,2,2,2,NA,3,3,3,3,3,3,3) The rules I want to use to create 'c' are: A numeric of equal to, or over 7 in 'b' needs to result in a numeric (i.e. not NA) in 'c'; A numeric of less than 7 in 'b' needs to result in NA in 'c'; Where 'groups' of numerics equal to, or over 7 in 'b' are present (i.e. next to each other in the list), the numerics produced in 'c' all need to be the same; Each 'group' of numerics in 'b' must result in a unique numeric in 'c' (and, ideally, they should run in sequence as in 'c' above (1,2,3...). If anyone has any idea where to start on this or can crack it I'll be most grateful!! Many thanks in advance, Ben Gillespie, Research Postgraduate o---o School of Geography, University of Leeds, Leeds, LS2 9JT o---o Tel: +44(0)113 34 33345 Mob: +44(0)770 868 7641 o---o http://www.geog.leeds.ac.uk/ o-o @RiversBenG o--o __ 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-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] Splitting times into groups based on a range of times
Hi all, I hope you can help with this one! I have a dataframe: 'df' that consists of a vector of times: 'dt2' and a vector of group id's: 'group': dates2=rep(01/02/13,times=8) times2=c(12:00:00,12:30:00,12:45:00,13:15:00,13:30:00,14:00:00,14:45:00,17:30:00) y =paste(dates2, times2) dt2=strptime(y, %m/%d/%y %H:%M:%S) group=c(1,1,2,2,3,3,4,4) df=data.frame(dt2,group) I also have a vector: 'dt' which is a series of times: dates=rep(01/02/13,times=20) times=c(12:00:00,12:15:00,12:30:00,12:45:00,13:00:00,13:15:00,13:30:00,13:45:00,14:00:00,14:15:00,14:30:00,14:45:00,15:00:00,15:15:00,15:30:00,15:45:00,16:00:00,16:15:00,16:30:00,16:45:00,17:00:00,17:15:00,17:30:00,17:45:00) x =paste(dates, times) dt=strptime(x, %m/%d/%y %H:%M:%S) I wish to create a vector which looks like 'id': id=c(1,1,1,2,2,2,3,3,3,0,0,4,4,4,4,4,4,4,4,4,4,4,4,0) The rules I wish to follow to create 'id' are: 1. If a value in 'dt' is either equal to, or, within the range of times within group x in dataframe 'df', then, the value in 'id' will equal x. So, for example, in 'df', group 4 is between the times of 14:45:00 and 17:30:00 on the 01/02/13. Thus, the 12th to 23rd value in 'id' equals 4 as these values correspond to times within 'dt' that are equal to and within the range of 14:45:00 and 17:30:00 on the 01/02/13. If this doesn't make sense, please ask, I'm not sure where to even start with this... possibly the 'cut' function? Many thanks in advance, Ben Gillespie, Research Postgraduate o---o School of Geography, University of Leeds, Leeds, LS2 9JT o---o Tel: +44(0)113 34 33345 Mob: +44(0)770 868 7641 o---o http://www.geog.leeds.ac.uk/ o-o @RiversBenG o--o __ 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] Possible loop/ if statement query
Dear r genii, I hope you can help. I have vector 'b': b=c((1:10),sort(1:9,decreasing=TRUE),(2:12),sort(6:11,decreasing=TRUE),(7:13)) and, from 'b' I wish to create vector 'c': c=c( NA,NA,NA,NA,NA,NA,1,1,1,1,1,1,1,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,2,2,2,2,2,2,2,2,2,2,2,NA,3,3,3,3,3,3,3) The rules I want to use to create 'c' are: A numeric of equal to, or over 7 in 'b' needs to result in a numeric (i.e. not NA) in 'c'; A numeric of less than 7 in 'b' needs to result in NA in 'c'; Where 'groups' of numerics equal to, or over 7 in 'b' are present (i.e. next to each other in the list), the numerics produced in 'c' all need to be the same; Each 'group' of numerics in 'b' must result in a unique numeric in 'c' (and, ideally, they should run in sequence as in 'c' above (1,2,3...). If anyone has any idea where to start on this or can crack it I'll be most grateful!! Many thanks in advance, Ben Gillespie, Research Postgraduate o---o School of Geography, University of Leeds, Leeds, LS2 9JT o---o Tel: +44(0)113 34 33345 Mob: +44(0)770 868 7641 o---o http://www.geog.leeds.ac.uk/ o-o @RiversBenG o--o __ 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] Statistical test for heteroscedasticity for an object of class gls
Hi there, Does anyone know of a statistical test for heteroscedasticity for an object of class gls? (or alternative objective methods). Thanks in advance, Ben Gillespie, Research Postgraduate o---o School of Geography, University of Leeds, Leeds, LS2 9JT o---o http://www.geog.leeds.ac.uk/ o-o @RiversBenG o--o __ 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] Statistical test for heteroscedasticity for an object of class gls
Also, out of interest, does anyone know of a test (or objective method) for spatial autocorrelation for gls objects? Thanks, Ben Gillespie, Research Postgraduate o---o School of Geography, University of Leeds, Leeds, LS2 9JT o---o http://www.geog.leeds.ac.uk/ o-o @RiversBenG o--o From: Benjamin Gillespie Sent: 18 April 2013 15:38 To: r-help@R-project.org Subject: Statistical test for heteroscedasticity for an object of class gls Hi there, Does anyone know of a statistical test for heteroscedasticity for an object of class gls? (or alternative objective methods). Thanks in advance, Ben Gillespie, Research Postgraduate o---o School of Geography, University of Leeds, Leeds, LS2 9JT o---o http://www.geog.leeds.ac.uk/ o-o @RiversBenG o--o __ 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] Superscript followed by number then superscript in text
Thanks to Dennis, Thomas and Rui - I was missing the ~. Many thanks, Ben Gillespie Research Postgraduate From: Dennis Murphy [djmu...@gmail.com] Sent: 18 March 2013 20:47 To: Benjamin Gillespie Subject: Re: [R] Superscript followed by number then superscript in text Hi: plot(1, 1) text(1, 0.8, expression(capacity~10^3~m^3)) Dennis On Mon, Mar 18, 2013 at 1:29 PM, Benjamin Gillespie gy...@leeds.ac.uk wrote: Hi all, I'm having problems finding the correct format for a command. I would like to write some text on a plot. I'm using the following command: text(x,y,text here, srt=90) I would like the text to read: capacity 10^3 m^3 (with ^ denoting superscript (i.e. each '3' as superscript). I've tried fiddling around with expression(paste(etc to no avail. I receive errors such as: Error: unexpected numeric constant... Anyone had experience with this before? Any suggestions would be great. Many thanks in advance, Ben Gillespie Research Postgraduate School of Geography University of Leeds Leeds LS2 9JT __ 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-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] Superscript followed by number then superscript in text
Hi all, I'm having problems finding the correct format for a command. I would like to write some text on a plot. I'm using the following command: text(x,y,text here, srt=90) I would like the text to read: capacity 10^3 m^3 (with ^ denoting superscript (i.e. each '3' as superscript). I've tried fiddling around with expression(paste(etc to no avail. I receive errors such as: Error: unexpected numeric constant... Anyone had experience with this before? Any suggestions would be great. Many thanks in advance, Ben Gillespie Research Postgraduate School of Geography University of Leeds Leeds LS2 9JT __ 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] Script for conditional sums of vectors
Hi guys, I hope you can help me with this (probably) simple query: I have a data frame: -- a=c(1,1,1,1,1,1,2,2,2,2,2,2) b=c(1,1,1,2,3,4,1,1,2,2,3,4) c=c(400,200,300,100,500,300,200,100,500,400,200,100) data=data.frame(a=a,b=b,c=c) -- And I would like to get the following output: -- b a 1 2 3 4 1 900 100 500 300 2 300 900 200 100 -- The values in the output represent the sum of values c in data frame data, for each a and b combination. For example, where a = 1 and b = 1, the output is 400+200+300 = 900. Please would anyone be able to provide a script to create my desired output? Many thanks in advance, Ben Gillespie Research Postgraduate School of Geography University of Leeds Leeds LS2 9JT __ 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] Script for conditional sums of vectors
Thanks everyone - this seems to be the most efficient answer, I'll do a bit of reading around too as suggested. Thanks again, Ben Gillespie Research Postgraduate School of Geography University of Leeds Leeds LS2 9JT From: D. Rizopoulos [d.rizopou...@erasmusmc.nl] Sent: 04 February 2013 09:35 To: Benjamin Gillespie Cc: r-help@r-project.org Subject: Re: [R] Script for conditional sums of vectors try this: a - c(1,1,1,1,1,1,2,2,2,2,2,2) b - c(1,1,1,2,3,4,1,1,2,2,3,4) c - c(400,200,300,100,500,300,200,100,500,400,200,100) DF - data.frame(a, b, c) with(DF, tapply(c, list(a, b), sum)) I hope it helps. Best, Dimitris On 2/4/2013 10:29 AM, Benjamin Gillespie wrote: Hi guys, I hope you can help me with this (probably) simple query: I have a data frame: -- a=c(1,1,1,1,1,1,2,2,2,2,2,2) b=c(1,1,1,2,3,4,1,1,2,2,3,4) c=c(400,200,300,100,500,300,200,100,500,400,200,100) data=data.frame(a=a,b=b,c=c) -- And I would like to get the following output: -- b a 1 2 3 4 1 900 100 500 300 2 300 900 200 100 -- The values in the output represent the sum of values c in data frame data, for each a and b combination. For example, where a = 1 and b = 1, the output is 400+200+300 = 900. Please would anyone be able to provide a script to create my desired output? Many thanks in advance, Ben Gillespie Research Postgraduate School of Geography University of Leeds Leeds LS2 9JT __ 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. -- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 Web: http://www.erasmusmc.nl/biostatistiek/ __ 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] Code to fetch summary info from vector
Hi all, Thanks in advance for any help. I have a vector b: b=c(1,1,1,2,3,4,3,2,1,1,1,1,1,2,3,4,5,4,3.5,3,2,1,1,1) Imagine b is river flow throughout time. I would like some code that will generate the following information: number of individual 'periods' where b1 (= 2 in this case) period 1 length = 5, max = 4 period 2 length = 8, max = 5 I can't figure anything useful out. Thanks, Ben Gillespie Research Postgraduate School of Geography University of Leeds Leeds LS2 9JT http://www.geog.leeds.ac.uk/ __ 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] Code to fetch summary info from vector
Thanks everyone, I've used the code Will supplied - this worked well. Thanks to all the others who contributed. Ben Gillespie Research Postgraduate School of Geography University of Leeds Leeds LS2 9JT Tel: +44(0)113 34 33345 Mob: +44(0)770 868 7641 http://www.geog.leeds.ac.uk/ From: William Dunlap [wdun...@tibco.com] Sent: 15 January 2013 16:51 To: Benjamin Gillespie; r-help@r-project.org Subject: RE: Code to fetch summary info from vector I don't completely understand the question, but if you are looking for the lengths of the runs of values greater than 1 then rle() would help: b - c(1,1,1,2,3,4,3,2,1,1,1,1,1,2,3,4,5,4,3.5,3,2,1,1,1) r - rle(b1) r Run Length Encoding lengths: int [1:5] 3 5 5 8 3 values : logi [1:5] FALSE TRUE FALSE TRUE FALSE r$lengths[r$values] [1] 5 8 To get the maximum of each run of values greater than one, something like the following may do (there are more elegant ways, but I don't want to spend the time on it until I know what the question is): runNumber - cumsum( c(b[1]1, (b[-1]1) (b[-length(b)]=1)) ) runNumber[b=1] - NA tapply(b, runNumber, max) 1 2 4 5 Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of Benjamin Gillespie Sent: Tuesday, January 15, 2013 8:16 AM To: r-help@r-project.org Subject: [R] Code to fetch summary info from vector Hi all, Thanks in advance for any help. I have a vector b: b=c(1,1,1,2,3,4,3,2,1,1,1,1,1,2,3,4,5,4,3.5,3,2,1,1,1) Imagine b is river flow throughout time. I would like some code that will generate the following information: number of individual 'periods' where b1 (= 2 in this case) period 1 length = 5, max = 4 period 2 length = 8, max = 5 I can't figure anything useful out. Thanks, Ben Gillespie Research Postgraduate School of Geography University of Leeds Leeds LS2 9JT http://www.geog.leeds.ac.uk/ __ 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-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] Script to count unique values from two linked matricies
Hi, Thanks for helping me with this one. To save you time, the following is the code for the tables I uploaded as jpegs that you may not have received: dat1-data.frame(Species=paste(Species,1:3),Site1=c(5,NA,4),Site2=c(3,2,4),Site3=c(NA,5,NA)) dat2-data.frame(Species=paste(Species,1:3),TraitType1=c(1,2,3),TraitType2=c(2,4,2),TraitType3=c(5,1,1)) dat3-data.frame(Site=paste(Site,1:3),Trait_Richness=c(5,7,3)) So, I have two matricies, dat1 and dat2. Dat1 is a species abundance matrix. Dat2 is a species trait matrix. I want to create dat3 through use of a script. Dat 3 is a count of unique traits observed at each site. i.e. at site 1, species 1 and 3 are present (ass seen in dat1). Species 1 has traits: 1, 2 and 5 for trait types 1, 2 and 3 respectively. Species 3 has traits: 3, 2 and 1 for trait types 1, 2 and 3 respectively. So, at site 1: For trait type 1, 2 unique traits were observed. For trait type 2, 1 unique trait was observed (both species 1 and 3 were classed as 1) and for trait type 3, 2 unique traits (trait richness) were observed; thus, 2+1+2=5. and so on... so at site 2, all three species were observed... For trait type 1, 3 unique traits were observed (1, 2, 3), for trait type 2, 2 unique traits were observed (2, 4, 2) and for trait type 3, 2 unique traits were observed (5, 1, 1). So, for site 2, trait richness is 7 (3+2+2) traits. I hope this helps to explain, please let me know if you need any further information, Ben Gillespie Research Postgraduate School of Geography University of Leeds Leeds LS2 9JT Tel: +44(0)113 34 33345 Mob: +44(0)770 868 7641 http://www.geog.leeds.ac.uk/ From: arun [smartpink...@yahoo.com] Sent: 24 September 2012 19:36 To: Benjamin Gillespie Subject: Re: [R] Script to count unique values from two linked matricies HI Ben, Sorry,I couldn't understand how you counted the trait richness. Could you elaborate? A.K. - Original Message - From: benrgillespie gy...@leeds.ac.uk To: r-help@r-project.org Cc: Sent: Monday, September 24, 2012 7:47 AM Subject: [R] Script to count unique values from two linked matricies I hope you can help with this one. I have two matricies: 1. A species abundance matrix: http://r.789695.n4.nabble.com/file/n4643979/2species_matrix.jpg 2. A species trait score matrix: http://r.789695.n4.nabble.com/file/n4643979/2trait_matrix.jpg The trait matrix lists trait scores for each species as listed in the species abundance matrix. I would like to create a script that would effectively count the unique traits (trait richness) for each site and produce an output like this: http://r.789695.n4.nabble.com/file/n4643979/trait_richness.jpg Firstly, is this possible in R? Secondly, if so, how would you go about writing a script to achieve my aim? Many thanks in advance, please let me know if you need further information. Ben Gillespie (Research Postgraduate) -- View this message in context: http://r.789695.n4.nabble.com/Script-to-count-unique-values-from-two-linked-matricies-tp4643979.html Sent from the R help mailing list archive at Nabble.com. __ 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-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] Script to count unique values from two linked matricies
Fantastic help - thanks guys. Heramb, Pikal and Rui all came up with solutions. I prefer Rui's as it seems the simplest. I'm going to try and work through them to understand exactly what's going on at each stage. Thanks again all, Ben Gillespie Research Postgraduate School of Geography University of Leeds Leeds LS2 9JT Tel: +44(0)113 34 33345 Mob: +44(0)770 868 7641 http://www.geog.leeds.ac.uk/ From: Heramb Gadgil [heramb.gad...@gmail.com] Sent: 25 September 2012 10:38 To: Benjamin Gillespie Cc: arun; r-help@r-project.org Subject: Re: [R] Script to count unique values from two linked matricies We can have something like this; Data1-data.frame(Site1=c(5,NA,4),Site2=c(3,2,4),Site3=c(NA,5,NA)) Data2-data.frame(TraitType1=c(1,2,3),TraitType2=c(2,4,2),TraitType3=c(5,1,1)) rownames(Data1)-paste(Species,1:3) rownames(Data2)-paste(Species,1:3) User_Defined=function(dat1,dat2){c1=ncol(dat1) out=NULL dummy=lapply(1:c1,function(x){step1=dat1[,x] step2=dat2[which(is.nahttp://is.na(step1)==F),] step3=length(unlist(apply(step2,2,unique))) out-c(out,step3) }) Final-data.frame(TraitRichness=out) rownames(Final)-paste('Site',1:3) Final_Table-Final } User_Defined(Data1,Data2) Final_Table I hope this is what you need. Best, Heramb On Tue, Sep 25, 2012 at 1:41 PM, Benjamin Gillespie gy...@leeds.ac.ukmailto:gy...@leeds.ac.uk wrote: Hi, Thanks for helping me with this one. To save you time, the following is the code for the tables I uploaded as jpegs that you may not have received: dat1-data.frame(Species=paste(Species,1:3),Site1=c(5,NA,4),Site2=c(3,2,4),Site3=c(NA,5,NA)) dat2-data.frame(Species=paste(Species,1:3),TraitType1=c(1,2,3),TraitType2=c(2,4,2),TraitType3=c(5,1,1)) dat3-data.frame(Site=paste(Site,1:3),Trait_Richness=c(5,7,3)) So, I have two matricies, dat1 and dat2. Dat1 is a species abundance matrix. Dat2 is a species trait matrix. I want to create dat3 through use of a script. Dat 3 is a count of unique traits observed at each site. i.e. at site 1, species 1 and 3 are present (ass seen in dat1). Species 1 has traits: 1, 2 and 5 for trait types 1, 2 and 3 respectively. Species 3 has traits: 3, 2 and 1 for trait types 1, 2 and 3 respectively. So, at site 1: For trait type 1, 2 unique traits were observed. For trait type 2, 1 unique trait was observed (both species 1 and 3 were classed as 1) and for trait type 3, 2 unique traits (trait richness) were observed; thus, 2+1+2=5. and so on... so at site 2, all three species were observed... For trait type 1, 3 unique traits were observed (1, 2, 3), for trait type 2, 2 unique traits were observed (2, 4, 2) and for trait type 3, 2 unique traits were observed (5, 1, 1). So, for site 2, trait richness is 7 (3+2+2) traits. I hope this helps to explain, please let me know if you need any further information, Ben Gillespie Research Postgraduate School of Geography University of Leeds Leeds LS2 9JT Tel: +44(0)113 34 33345 Mob: +44(0)770 868 7641 http://www.geog.leeds.ac.uk/ From: arun [smartpink...@yahoo.commailto:smartpink...@yahoo.com] Sent: 24 September 2012 19:36 To: Benjamin Gillespie Subject: Re: [R] Script to count unique values from two linked matricies HI Ben, Sorry,I couldn't understand how you counted the trait richness. Could you elaborate? A.K. - Original Message - From: benrgillespie gy...@leeds.ac.ukmailto:gy...@leeds.ac.uk To: r-help@r-project.orgmailto:r-help@r-project.org Cc: Sent: Monday, September 24, 2012 7:47 AM Subject: [R] Script to count unique values from two linked matricies I hope you can help with this one. I have two matricies: 1. A species abundance matrix: http://r.789695.n4.nabble.com/file/n4643979/2species_matrix.jpg 2. A species trait score matrix: http://r.789695.n4.nabble.com/file/n4643979/2trait_matrix.jpg The trait matrix lists trait scores for each species as listed in the species abundance matrix. I would like to create a script that would effectively count the unique traits (trait richness) for each site and produce an output like this: http://r.789695.n4.nabble.com/file/n4643979/trait_richness.jpg Firstly, is this possible in R? Secondly, if so, how would you go about writing a script to achieve my aim? Many thanks in advance, please let me know if you need further information. Ben Gillespie (Research Postgraduate) -- View this message in context: http://r.789695.n4.nabble.com/Script-to-count-unique-values-from-two-linked-matricies-tp4643979.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.orgmailto: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-help@r-project.orgmailto:R-help
Re: [R] Script to count unique values from two linked matricies
Excellent - thanks so much. Ben Gillespie Research Postgraduate School of Geography University of Leeds Leeds LS2 9JT Tel: +44(0)113 34 33345 Mob: +44(0)770 868 7641 http://www.geog.leeds.ac.uk/ From: arun [smartpink...@yahoo.com] Sent: 25 September 2012 14:22 To: Benjamin Gillespie Cc: PIKAL Petr; Rui Barradas; Heramb Gadgil; R help Subject: Re: [R] Script to count unique values from two linked matricies Hi Ben, Just a modification of Petr's solution with library(data.table): dat1-data.frame(Species=paste0(Species,1:3),Site1=c(5,NA,4),Site2=c(3,2,4),Site3=c(NA,5,NA)) dat2-data.frame(Species=paste0(Species,1:3),Trait1=1:3,Trait2=c(2,4,2),Trait3=c(5,1,1)) library(data.table) library(reshape) datm-merge(melt(dat1),melt(dat2),by=Species) temp-list(data.table(datm[datm[,2]==Site1 !is.na(datm[,3]),]),data.table(datm[datm[,2]==Site2 !is.na(datm[,3]),]),data.table(datm[datm[,2]==Site3 !is.na(datm[,3]),])) Trait_Richness-unlist(lapply(temp, function(x) sum(x[,list(length(unique(value.y))),list(variable.y)]$V1))) dat3-data.frame(Site=colnames(dat1)[2:4],Trait_Richness=Trait_Richness) dat3 # Site Trait_Richness #1 Site1 5 #2 Site2 7 #3 Site3 3 A.K. - Original Message - From: Benjamin Gillespie gy...@leeds.ac.uk To: Heramb Gadgil heramb.gad...@gmail.com Cc: arun smartpink...@yahoo.com; r-help@r-project.org r-help@r-project.org Sent: Tuesday, September 25, 2012 8:56 AM Subject: RE: [R] Script to count unique values from two linked matricies Fantastic help - thanks guys. Heramb, Pikal and Rui all came up with solutions. I prefer Rui's as it seems the simplest. I'm going to try and work through them to understand exactly what's going on at each stage. Thanks again all, Ben Gillespie Research Postgraduate School of Geography University of Leeds Leeds LS2 9JT Tel: +44(0)113 34 33345 Mob: +44(0)770 868 7641 http://www.geog.leeds.ac.uk/ From: Heramb Gadgil [heramb.gad...@gmail.com] Sent: 25 September 2012 10:38 To: Benjamin Gillespie Cc: arun; r-help@r-project.org Subject: Re: [R] Script to count unique values from two linked matricies We can have something like this; Data1-data.frame(Site1=c(5,NA,4),Site2=c(3,2,4),Site3=c(NA,5,NA)) Data2-data.frame(TraitType1=c(1,2,3),TraitType2=c(2,4,2),TraitType3=c(5,1,1)) rownames(Data1)-paste(Species,1:3) rownames(Data2)-paste(Species,1:3) User_Defined=function(dat1,dat2){c1=ncol(dat1) out=NULL dummy=lapply(1:c1,function(x){step1=dat1[,x] step2=dat2[which(is.nahttp://is.na(step1)==F),] step3=length(unlist(apply(step2,2,unique))) out-c(out,step3) }) Final-data.frame(TraitRichness=out) rownames(Final)-paste('Site',1:3) Final_Table-Final } User_Defined(Data1,Data2) Final_Table I hope this is what you need. Best, Heramb On Tue, Sep 25, 2012 at 1:41 PM, Benjamin Gillespie gy...@leeds.ac.ukmailto:gy...@leeds.ac.uk wrote: Hi, Thanks for helping me with this one. To save you time, the following is the code for the tables I uploaded as jpegs that you may not have received: dat1-data.frame(Species=paste(Species,1:3),Site1=c(5,NA,4),Site2=c(3,2,4),Site3=c(NA,5,NA)) dat2-data.frame(Species=paste(Species,1:3),TraitType1=c(1,2,3),TraitType2=c(2,4,2),TraitType3=c(5,1,1)) dat3-data.frame(Site=paste(Site,1:3),Trait_Richness=c(5,7,3)) So, I have two matricies, dat1 and dat2. Dat1 is a species abundance matrix. Dat2 is a species trait matrix. I want to create dat3 through use of a script. Dat 3 is a count of unique traits observed at each site. i.e. at site 1, species 1 and 3 are present (ass seen in dat1). Species 1 has traits: 1, 2 and 5 for trait types 1, 2 and 3 respectively. Species 3 has traits: 3, 2 and 1 for trait types 1, 2 and 3 respectively. So, at site 1: For trait type 1, 2 unique traits were observed. For trait type 2, 1 unique trait was observed (both species 1 and 3 were classed as 1) and for trait type 3, 2 unique traits (trait richness) were observed; thus, 2+1+2=5. and so on... so at site 2, all three species were observed... For trait type 1, 3 unique traits were observed (1, 2, 3), for trait type 2, 2 unique traits were observed (2, 4, 2) and for trait type 3, 2 unique traits were observed (5, 1, 1). So, for site 2, trait richness is 7 (3+2+2) traits. I hope this helps to explain, please let me know if you need any further information, Ben Gillespie Research Postgraduate School of Geography University of Leeds Leeds LS2 9JT Tel: +44(0)113 34 33345 Mob: +44(0)770 868 7641 http://www.geog.leeds.ac.uk/ From: arun [smartpink...@yahoo.commailto:smartpink...@yahoo.com] Sent: 24 September 2012 19:36 To: Benjamin Gillespie Subject: Re: [R] Script to count unique values from two linked matricies HI Ben, Sorry,I couldn't understand how you counted the trait richness. Could you elaborate? A.K. - Original Message - From: benrgillespie gy
Re: [R] Script to count unique values from two linked matricies
You're good! Thanks again :) Ben Gillespie Research Postgraduate School of Geography University of Leeds Leeds LS2 9JT Tel: +44(0)113 34 33345 Mob: +44(0)770 868 7641 http://www.geog.leeds.ac.uk/ From: arun [smartpink...@yahoo.com] Sent: 25 September 2012 17:04 To: Benjamin Gillespie Cc: R help Subject: Re: [R] Script to count unique values from two linked matricies Hi Ben, No problem. The code could be collapsed to 2 steps: library(data.table) library(reshape) datm-merge(melt(dat1),melt(dat2),by=Species) dat3-data.frame(unlist(lapply(lapply(split(datm,datm$variable.x),function(x) data.table(x[!is.na(x$value.x),])),function(x) sum(x[,list(length(unique(value.y))),list(variable.y)]$V1 colnames(dat3)-Trait_Richness dat3 # Trait_Richness #Site1 5 #Site2 7 #Site3 3 A.K. - Original Message - From: Benjamin Gillespie gy...@leeds.ac.uk To: arun smartpink...@yahoo.com Cc: PIKAL Petr petr.pi...@precheza.cz; Rui Barradas ruipbarra...@sapo.pt; Heramb Gadgil heramb.gad...@gmail.com; R help r-help@r-project.org Sent: Tuesday, September 25, 2012 9:34 AM Subject: RE: [R] Script to count unique values from two linked matricies Excellent - thanks so much. Ben Gillespie Research Postgraduate School of Geography University of Leeds Leeds LS2 9JT Tel: +44(0)113 34 33345 Mob: +44(0)770 868 7641 http://www.geog.leeds.ac.uk/ From: arun [smartpink...@yahoo.com] Sent: 25 September 2012 14:22 To: Benjamin Gillespie Cc: PIKAL Petr; Rui Barradas; Heramb Gadgil; R help Subject: Re: [R] Script to count unique values from two linked matricies Hi Ben, Just a modification of Petr's solution with library(data.table): dat1-data.frame(Species=paste0(Species,1:3),Site1=c(5,NA,4),Site2=c(3,2,4),Site3=c(NA,5,NA)) dat2-data.frame(Species=paste0(Species,1:3),Trait1=1:3,Trait2=c(2,4,2),Trait3=c(5,1,1)) library(data.table) library(reshape) datm-merge(melt(dat1),melt(dat2),by=Species) temp-list(data.table(datm[datm[,2]==Site1 !is.na(datm[,3]),]),data.table(datm[datm[,2]==Site2 !is.na(datm[,3]),]),data.table(datm[datm[,2]==Site3 !is.na(datm[,3]),])) Trait_Richness-unlist(lapply(temp, function(x) sum(x[,list(length(unique(value.y))),list(variable.y)]$V1))) dat3-data.frame(Site=colnames(dat1)[2:4],Trait_Richness=Trait_Richness) dat3 # Site Trait_Richness #1 Site1 5 #2 Site2 7 #3 Site3 3 A.K. - Original Message - From: Benjamin Gillespie gy...@leeds.ac.uk To: Heramb Gadgil heramb.gad...@gmail.com Cc: arun smartpink...@yahoo.com; r-help@r-project.org r-help@r-project.org Sent: Tuesday, September 25, 2012 8:56 AM Subject: RE: [R] Script to count unique values from two linked matricies Fantastic help - thanks guys. Heramb, Pikal and Rui all came up with solutions. I prefer Rui's as it seems the simplest. I'm going to try and work through them to understand exactly what's going on at each stage. Thanks again all, Ben Gillespie Research Postgraduate School of Geography University of Leeds Leeds LS2 9JT Tel: +44(0)113 34 33345 Mob: +44(0)770 868 7641 http://www.geog.leeds.ac.uk/ From: Heramb Gadgil [heramb.gad...@gmail.com] Sent: 25 September 2012 10:38 To: Benjamin Gillespie Cc: arun; r-help@r-project.org Subject: Re: [R] Script to count unique values from two linked matricies We can have something like this; Data1-data.frame(Site1=c(5,NA,4),Site2=c(3,2,4),Site3=c(NA,5,NA)) Data2-data.frame(TraitType1=c(1,2,3),TraitType2=c(2,4,2),TraitType3=c(5,1,1)) rownames(Data1)-paste(Species,1:3) rownames(Data2)-paste(Species,1:3) User_Defined=function(dat1,dat2){c1=ncol(dat1) out=NULL dummy=lapply(1:c1,function(x){step1=dat1[,x] step2=dat2[which(is.nahttp://is.na(step1)==F),] step3=length(unlist(apply(step2,2,unique))) out-c(out,step3) }) Final-data.frame(TraitRichness=out) rownames(Final)-paste('Site',1:3) Final_Table-Final } User_Defined(Data1,Data2) Final_Table I hope this is what you need. Best, Heramb On Tue, Sep 25, 2012 at 1:41 PM, Benjamin Gillespie gy...@leeds.ac.ukmailto:gy...@leeds.ac.uk wrote: Hi, Thanks for helping me with this one. To save you time, the following is the code for the tables I uploaded as jpegs that you may not have received: dat1-data.frame(Species=paste(Species,1:3),Site1=c(5,NA,4),Site2=c(3,2,4),Site3=c(NA,5,NA)) dat2-data.frame(Species=paste(Species,1:3),TraitType1=c(1,2,3),TraitType2=c(2,4,2),TraitType3=c(5,1,1)) dat3-data.frame(Site=paste(Site,1:3),Trait_Richness=c(5,7,3)) So, I have two matricies, dat1 and dat2. Dat1 is a species abundance matrix. Dat2 is a species trait matrix. I want to create dat3 through use of a script. Dat 3 is a count of unique traits observed at each site. i.e. at site 1, species 1 and 3 are present (ass seen in dat1). Species 1 has traits: 1, 2 and 5 for trait types 1, 2 and 3 respectively. Species 3 has traits
Re: [R] Script to count unique values from two linked matricies
Hi Petr, Please see the attached .csv files - perhaps these will help, Thanks, Ben Gillespie Research Postgraduate School of Geography University of Leeds Leeds LS2 9JT Tel: +44(0)113 34 33345 Mob: +44(0)770 868 7641 http://www.geog.leeds.ac.uk/ From: PIKAL Petr [petr.pi...@precheza.cz] Sent: 24 September 2012 13:34 To: Benjamin Gillespie; r-help@r-project.org Subject: RE: [R] Script to count unique values from two linked matricies Hi I have no access to Nabble so it is difficult to understand what do you want. -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-bounces@r- project.org] On Behalf Of benrgillespie Sent: Monday, September 24, 2012 1:47 PM To: r-help@r-project.org Subject: [R] Script to count unique values from two linked matricies I hope you can help with this one. I have two matricies: 1. A species abundance matrix: http://r.789695.n4.nabble.com/file/n4643979/2species_matrix.jpg 2. A species trait score matrix: http://r.789695.n4.nabble.com/file/n4643979/2trait_matrix.jpg The trait matrix lists trait scores for each species as listed in the species abundance matrix. I would like to create a script that would effectively count the unique traits (trait richness) for each site and produce an output like this: http://r.789695.n4.nabble.com/file/n4643979/trait_richness.jpg Firstly, is this possible in R? Secondly, if so, how would you go about Why not, everything is possible with R. Maybe you could provide data by dput(species). Maybe you want something like ?table or ?aggregate. Without data it is hard to say. Regards Petr writing a script to achieve my aim? Many thanks in advance, please let me know if you need further information. Ben Gillespie (Research Postgraduate) -- View this message in context: http://r.789695.n4.nabble.com/Script-to- count-unique-values-from-two-linked-matricies-tp4643979.html Sent from the R help mailing list archive at Nabble.com. __ 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-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] List creation based on matrix
Please see the attached .csv files for further info, Thanks guys, Ben Gillespie Research Postgraduate School of Geography University of Leeds Leeds LS2 9JT Tel: +44(0)113 34 33345 Mob: +44(0)770 868 7641 http://www.geog.leeds.ac.uk/ From: PIKAL Petr [petr.pi...@precheza.cz] Sent: 24 September 2012 13:39 To: Benjamin Gillespie; r-help@r-project.org Subject: RE: [R] List creation based on matrix Hi -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-bounces@r- project.org] On Behalf Of benrgillespie Sent: Monday, September 24, 2012 1:20 PM To: r-help@r-project.org Subject: [R] List creation based on matrix Hi guys, It would be great if you could help me with this one... I'm looking to create a script to convert a matrix of species abundance e.g: http://r.789695.n4.nabble.com/file/n4643978/species_matrix.jpg into two vectors e.g: http://r.789695.n4.nabble.com/file/n4643978/communitylist.jpg If you feel there is no easy answer to this and that it would be easier to do in excel or open office calc (or another program), please let me Not much can be done easier in Excel. If you do not fail to provide data you could get better answers. Regards Petr know. Thanks in advance for your help and let me know if you require any further information, Ben Gillespie (Research Postgrad) -- View this message in context: http://r.789695.n4.nabble.com/List- creation-based-on-matrix-tp4643978.html Sent from the R help mailing list archive at Nabble.com. __ 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-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] List creation based on matrix
Hi Arun, I've just run through that - thanks so much, it worked perfectly. Learnt lots of new commands too :) Thanks for all your help - awesome! ps. the only thing I changed was to remove the '0' from the first line after 'paste'. Ben Gillespie Research Postgraduate School of Geography University of Leeds Leeds LS2 9JT Tel: +44(0)113 34 33345 Mob: +44(0)770 868 7641 http://www.geog.leeds.ac.uk/ From: arun [smartpink...@yahoo.com] Sent: 24 September 2012 14:01 To: Benjamin Gillespie Cc: PIKAL Petr; R help Subject: Re: [R] List creation based on matrix Hi, Couldn't find your attachment. But, from Nabble, I saw the data. Try this:dat1-data.frame(Species=paste0(Species,1:6),Site1=c(5,NA,4,NA,3,NA),Site2=c(4,NA,4,4,NA,4),Site3=c(NA,5,NA,NA,NA,NA),Site4=c(NA,NA,NA,6,5,NA)) dat2-melt(dat1) dat3-dat2[!is.na(dat2$value),] dat3-dat3[,1:2] colnames(dat3)[2]-Site dat3$Species-gsub(.*(\\d),\\1,dat3$Species) dat3$Site-gsub(.*(\\d),\\1,dat3$Site) dat4-dat3[,c(2,1)] dat4 rownames(dat4)-1:nrow(dat4) dat4 # Site Species #1 1 1 #2 1 3 #3 1 5 #4 2 1 #5 2 3 #6 2 4 #7 2 6 #8 3 2 #9 4 4 #104 5 A.K. - Original Message - From: Benjamin Gillespie gy...@leeds.ac.uk To: PIKAL Petr petr.pi...@precheza.cz; r-help@r-project.org r-help@r-project.org Cc: Sent: Monday, September 24, 2012 8:52 AM Subject: Re: [R] List creation based on matrix Please see the attached .csv files for further info, Thanks guys, Ben Gillespie Research Postgraduate School of Geography University of Leeds Leeds LS2 9JT Tel: +44(0)113 34 33345 Mob: +44(0)770 868 7641 http://www.geog.leeds.ac.uk/ From: PIKAL Petr [petr.pi...@precheza.cz] Sent: 24 September 2012 13:39 To: Benjamin Gillespie; r-help@r-project.org Subject: RE: [R] List creation based on matrix Hi -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-bounces@r- project.org] On Behalf Of benrgillespie Sent: Monday, September 24, 2012 1:20 PM To: r-help@r-project.org Subject: [R] List creation based on matrix Hi guys, It would be great if you could help me with this one... I'm looking to create a script to convert a matrix of species abundance e.g: http://r.789695.n4.nabble.com/file/n4643978/species_matrix.jpg into two vectors e.g: http://r.789695.n4.nabble.com/file/n4643978/communitylist.jpg If you feel there is no easy answer to this and that it would be easier to do in excel or open office calc (or another program), please let me Not much can be done easier in Excel. If you do not fail to provide data you could get better answers. Regards Petr know. Thanks in advance for your help and let me know if you require any further information, Ben Gillespie (Research Postgrad) -- View this message in context: http://r.789695.n4.nabble.com/List- creation-based-on-matrix-tp4643978.html Sent from the R help mailing list archive at Nabble.com. __ 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-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-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] List creation based on matrix
Great, thanks, Ben Gillespie Research Postgraduate School of Geography University of Leeds Leeds LS2 9JT Tel: +44(0)113 34 33345 Mob: +44(0)770 868 7641 http://www.geog.leeds.ac.uk/ From: Jeff Newmiller [jdnew...@dcn.davis.ca.us] Sent: 24 September 2012 15:35 To: Benjamin Gillespie; arun Cc: R help Subject: Re: [R] List creation based on matrix If you don't have paste0() then you should upgrade to the current version of R. --- Jeff NewmillerThe . . Go Live... DCN:jdnew...@dcn.davis.ca.usBasics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/BatteriesO.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k --- Sent from my phone. Please excuse my brevity. Benjamin Gillespie gy...@leeds.ac.uk wrote: Hi Arun, I've just run through that - thanks so much, it worked perfectly. Learnt lots of new commands too :) Thanks for all your help - awesome! ps. the only thing I changed was to remove the '0' from the first line after 'paste'. Ben Gillespie Research Postgraduate School of Geography University of Leeds Leeds LS2 9JT Tel: +44(0)113 34 33345 Mob: +44(0)770 868 7641 http://www.geog.leeds.ac.uk/ From: arun [smartpink...@yahoo.com] Sent: 24 September 2012 14:01 To: Benjamin Gillespie Cc: PIKAL Petr; R help Subject: Re: [R] List creation based on matrix Hi, Couldn't find your attachment. But, from Nabble, I saw the data. Try this:dat1-data.frame(Species=paste0(Species,1:6),Site1=c(5,NA,4,NA,3,NA),Site2=c(4,NA,4,4,NA,4),Site3=c(NA,5,NA,NA,NA,NA),Site4=c(NA,NA,NA,6,5,NA)) dat2-melt(dat1) dat3-dat2[!is.na(dat2$value),] dat3-dat3[,1:2] colnames(dat3)[2]-Site dat3$Species-gsub(.*(\\d),\\1,dat3$Species) dat3$Site-gsub(.*(\\d),\\1,dat3$Site) dat4-dat3[,c(2,1)] dat4 rownames(dat4)-1:nrow(dat4) dat4 # Site Species #1 1 1 #2 1 3 #3 1 5 #4 2 1 #5 2 3 #6 2 4 #7 2 6 #8 3 2 #9 4 4 #104 5 A.K. - Original Message - From: Benjamin Gillespie gy...@leeds.ac.uk To: PIKAL Petr petr.pi...@precheza.cz; r-help@r-project.org r-help@r-project.org Cc: Sent: Monday, September 24, 2012 8:52 AM Subject: Re: [R] List creation based on matrix Please see the attached .csv files for further info, Thanks guys, Ben Gillespie Research Postgraduate School of Geography University of Leeds Leeds LS2 9JT Tel: +44(0)113 34 33345 Mob: +44(0)770 868 7641 http://www.geog.leeds.ac.uk/ From: PIKAL Petr [petr.pi...@precheza.cz] Sent: 24 September 2012 13:39 To: Benjamin Gillespie; r-help@r-project.org Subject: RE: [R] List creation based on matrix Hi -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-bounces@r- project.org] On Behalf Of benrgillespie Sent: Monday, September 24, 2012 1:20 PM To: r-help@r-project.org Subject: [R] List creation based on matrix Hi guys, It would be great if you could help me with this one... I'm looking to create a script to convert a matrix of species abundance e.g: http://r.789695.n4.nabble.com/file/n4643978/species_matrix.jpg into two vectors e.g: http://r.789695.n4.nabble.com/file/n4643978/communitylist.jpg If you feel there is no easy answer to this and that it would be easier to do in excel or open office calc (or another program), please let me Not much can be done easier in Excel. If you do not fail to provide data you could get better answers. Regards Petr know. Thanks in advance for your help and let me know if you require any further information, Ben Gillespie (Research Postgrad) -- View this message in context: http://r.789695.n4.nabble.com/List- creation-based-on-matrix-tp4643978.html Sent from the R help mailing list archive at Nabble.com. __ 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-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-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