[R] x-axis with month and year
Hey list! I have a csv-file with two variables: (Date,CMI) Date,CMI Jan-93,3.24 Feb-93,-2.56 . . . Dec-06, 8.25 When I want to plot this dataset, R is sorting the date in alphabetical order. Is there any way to tell R not to do it? I know it is probably an easy issue but I couldn't find a solution so far. I have seen so many tips already but can't apply it on my own dataset. Thank you very much! Stefan [[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] x-axis with month and year
Thanks for steering me in the right direction! However, I did it slightly different: require(zoo) x <- yearmon(1993 + seq(0, 167)/12) plot(mydata$CMI~x) Thanks! Stefan -Original Message- From: Gabor Grothendieck [mailto:[EMAIL PROTECTED] Sent: Friday, August 15, 2008 2:06 PM To: Schreiber, Stefan Cc: r-help@r-project.org Subject: Re: [R] x-axis with month and year Check out the zoo package: Lines <- "Date,CMI Jan-93,3.24 Feb-93,-2.56 Dec-06, 8.25" library(zoo) z <- read.zoo(textConnection(Lines), FUN = as.yearmon, format = "%b-%y", sep = ",", header = TRUE) plot(z) On Fri, Aug 15, 2008 at 1:12 PM, Schreiber, Stefan <[EMAIL PROTECTED]> wrote: > Hey list! > > I have a csv-file with two variables: (Date,CMI) > > Date,CMI > Jan-93,3.24 > Feb-93,-2.56 > . > . > . > Dec-06, 8.25 > > > When I want to plot this dataset, R is sorting the date in alphabetical > order. Is there any way to tell R not to do it? > > I know it is probably an easy issue but I couldn't find a solution so > far. I have seen so many tips already but can't apply it on my own > dataset. > > Thank you very much! > > Stefan > >[[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-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] multi panel plot using xyplot()
Hi there, I hope you guys can help me with the following: If have a file like this: yearclone codeheight 19954 4-1 1 19964 4-1 2 19974 4-1 3 19954 4-2 1 19964 4-2 2 19974 4-2 3 19955 5-1 1 19965 5-1 2 19975 5-1 3 19955 5-2 1 19965 5-2 2 19975 5-2 3 . . . . . . . . . . . . 1. I want to analyze the growth of 52 different clones replicated 25 times over a period of 19 years. Now I'd like to plot all replicates per clone for the given period to compare them. 2. I did this first to subset all replicates per clone: dat<-vector('list',52) for (i in 1:52){ dat[[i]]<-subset(x,clone==i) } 3. Now I can plot all replicates per clone quite nicely using this command (for example clone 24): xyplot(ht~year, data=dat[[24]], groups=code,type="o",ylim=c(0,20), xlab="Year",ylab="Absolute Height [m]",auto.key=list(space = "right", points = FALSE, lines = TRUE)) My question is now whether there is a way to arrange these 52 plots in 6 plots per page? The argument par(mfrow=c(2,3)) isn't working here. Or does someone even has a better suggestion? THANKS A LOT! Stefan [[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] ANOVA on residuals to check for equality of variances
Hi all, I know that I can run an one way ANOVA on the absolute residual values to check the assumption of equal variances: model<- lm(y~x) summary(lm(abs(model$resid~x))) What if I have two factors? As far as I know I have to check this assumption on very factor/level combination. So if I have 3 factors with three levels, I would have to test 9 factor/level combinations. Here comes my questions: Can I do the same thing like above, except, including the "multiply" sign between the factors, check the overall significance (for equal variances) and go from there? model<- lm(y~x1*x2) summary(lm(abs(model$resid~x1*x2))). Right now I am using levene.test() from the 'lawstat' package and just adding another column to my data: mydata$group<-rep(letters[1:9],each=5), where letters[1:9] are for 9 factor/level combination and each=5 since I have 5 replicates each. Then I run levene.test(mydata$y, mydata$group, location="mean") and it works fine. Thanks for any answers on that! Stefan [[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] factor level issue after subsetting
Dear list, I cannot figure out why, after sub-setting my data, that particular item which I don't want to plot is still in the newly created subset (please see example below). R somehow remembers what was in the original data set. A work around is exporting and importing the new subset. Then it's all fine; but I don't like this idea and was wondering what am I missing here? Thanks! Stefan P.S. I am using R 2.13.2 for Mac. > dat<-read.csv("~/MyFiles/data.csv") > class(dat$treat) [1] "factor" > dat treat yield 1 cont 98.7 2 cont 97.2 3 cont 96.1 4 cont 98.1 5 10 103.0 6 10 101.3 7 10 102.1 8 10 101.9 9 30 121.1 1030 123.1 1130 119.7 1230 118.9 1360 109.9 1460 110.1 1560 113.1 1660 112.3 > plot(dat$treat,dat$yield) > dat.sub<-dat[which(dat$treat!='cont')] > class(dat.sub$treat) [1] "factor" > dat.sub treat yield 5 10 103.0 6 10 101.3 7 10 102.1 8 10 101.9 9 30 121.1 1030 123.1 1130 119.7 1230 118.9 1360 109.9 1460 110.1 1560 113.1 1660 112.3 > plot(dat.sub$treat,dat.sub$yield) [[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] factor level issue after subsetting
Thanks for the fast response and your comments! That works perfect! Another little mystery solved ;) Stefan From: Felipe Carrillo [mailto:mazatlanmex...@yahoo.com] Sent: Tuesday, November 01, 2011 3:54 PM To: Schreiber, Stefan; r-help@r-project.org Subject: Re: [R] factor level issue after subsetting Stefan: Use the droplevels function... dat <- read.table(textConnection(" treat yield 1 cont 98.7 2 cont 97.2 3 cont 96.1 4 cont 98.1 510 103.0 610 101.3 710 102.1 810 101.9 930 121.1 1030 123.1 1130 119.7 1230 118.9 1360 109.9 1460 110.1 1560 113.1 1660 112.3"),header=T) dat plot(dat$treat,dat$yield) dat.sub <- subset(dat,treat!="cont");dat.sub dat.sub <- droplevels(dat.sub)# drop unwanted levels plot(dat.sub$treat,dat.sub$yield) Felipe D. Carrillo Supervisory Fishery Biologist Department of the Interior US Fish & Wildlife Service California, USA http://www.fws.gov/redbluff/rbdd_jsmp.aspx From: "Schreiber, Stefan" To: r-help@r-project.org Sent: Tuesday, November 1, 2011 2:28 PM Subject: [R] factor level issue after subsetting Dear list, I cannot figure out why, after sub-setting my data, that particular item which I don't want to plot is still in the newly created subset (please see example below). R somehow remembers what was in the original data set. A work around is exporting and importing the new subset. Then it's all fine; but I don't like this idea and was wondering what am I missing here? Thanks! Stefan P.S. I am using R 2.13.2 for Mac. > dat<-read.csv("~/MyFiles/data.csv") > class(dat$treat) [1] "factor" > dat treat yield 1 cont 98.7 2 cont 97.2 3 cont 96.1 4 cont 98.1 510 103.0 610 101.3 710 102.1 810 101.9 930 121.1 1030 123.1 1130 119.7 1230 118.9 1360 109.9 1460 110.1 1560 113.1 1660 112.3 > plot(dat$treat,dat$yield) > dat.sub<-dat[which(dat$treat!='cont')] > class(dat.sub$treat) [1] "factor" > dat.sub treat yield 510 103.0 610 101.3 710 102.1 810 101.9 930 121.1 1030 123.1 1130 119.7 1230 118.9 1360 109.9 1460 110.1 1560 113.1 1660 112.3 > plot(dat.sub$treat,dat.sub$yield) [[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.
[R] Predicting x from y
Dear list members, I have just a quick question: I fitted a non-linear model y=a/x+b to describe my data (x=temperature and y=damage in %) and it works really nicely (see example below). I have 7 different species and 8 individuals per species. I measured damage for each individual per species at 4 different temperatures (e.g. -5, -10, -20, -40). Using the individuals per species, I fitted one model per species. Now I'd like to use the fitted model to go back and predict the temperature that causes 50% damage (and it's error). Basically, it pretty easy by just rearranging the formula to x=a/(y-b). But that way I don't get a measure of that temperature's error, do I? Can I use the residual standard error that R gave me for the non-linear model fit? Or do I have to fit 8 lines (each individual) per species, calculate x based on the 8 individuals and then take the mean? Unfortunately, dose.p from the MASS package doesn't work for non-linear models. When I take the log(abs(x)) the relationship becomes not satisfactory linear either. Any suggestions are highly appreciated! Thank you! Stefan EXAMPLE for species #1: y.damage<-c(5.7388985,1.7813519,3.7321461,2.9671031, 0.3223196,0.3207941,-1.4197658,-5.3472160, 41.1826677,29.3115243,31.3208841,35.3934115, 58.5848778,31.1541049,42.2983479,27.0615648, 64.1037728,54.7003353,66.7317044,65.4725881, 72.5755056,67.2683495,64.8717942,65.9603073, 75.0762273,56.7041960,60.0049429,70.0286506, 73.2801947,72.7015642,75.0944694,81.0361280) x.temp<-c(-5,-5,-5,-5,-5,-5,-5,-5,-10,-10,-10,-10,-10,-10,-10, -10,-20,-20,-20,-20,-20,-20,-20,-20,-40,-40,-40,-40,-40, -40,-40,-40) nls(y.damage~a/x.temp+b,start=list(a=400,b=80)) plot(y.damage~x.temp,xlab='Temperature',ylab='Damage [%]') curve(409.61/x+81.84,from=min(x.temp),to=max(x.temp),add=T) [[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] Predicting x from y
Thanks Ted! I really appreciate your time! Thanks for the link about the 'problem of calibration', and your suggestion to reformulate my model. I had no idea about it before. I certainly learnt something today. I will try your suggestions later today and let you know how it works out. Stefan -Original Message- From: r-help-boun...@r-project.org on behalf of ted.hard...@wlandres.net Sent: Fri 11/11/2011 4:07 PM To: r-help@r-project.org Subject: Re: [R] Predicting x from y Follow-up: See at end. On 11-Nov-11 21:16:02, Ted Harding wrote: > On 11-Nov-11 14:51:19, Schreiber, Stefan wrote: >> Dear list members, >> >> I have just a quick question: >> >> I fitted a non-linear model y=a/x+b to describe my data >> (x=temperature and y=damage in %) and it works really nicely >> (see example below). I have 7 different species and 8 individuals >> per species. I measured damage for each individual per species >> at 4 different temperatures (e.g. -5, -10, -20, -40). >> Using the individuals per species, I fitted one model per species. >> Now I'd like to use the fitted model to go back and predict the >> temperature that causes 50% damage (and it's error). Basically, >> it pretty easy by just rearranging the formula to x=a/(y-b). >> But that way I don't get a measure of that temperature's error, >> do I? Can I use the residual standard error that R gave me for >> the non-linear model fit? Or do I have to fit 8 lines (each >> individual) per species, calculate x based on the 8 individuals >> and then take the mean? >> >> Unfortunately, dose.p from the MASS package doesn't work for >> non-linear models. When I take the log(abs(x)) the relationship >> becomes not satisfactory linear either. >> >> Any suggestions are highly appreciated! >> >> Thank you! >> Stefan >> >> EXAMPLE for species #1: >> >> y.damage<-c(5.7388985,1.7813519,3.7321461,2.9671031, >> 0.3223196,0.3207941,-1.4197658,-5.3472160, >> 41.1826677,29.3115243,31.3208841,35.3934115, >> 58.5848778,31.1541049,42.2983479,27.0615648, >> 64.1037728,54.7003353,66.7317044,65.4725881, >> 72.5755056,67.2683495,64.8717942,65.9603073, >> 75.0762273,56.7041960,60.0049429,70.0286506, >> 73.2801947,72.7015642,75.0944694,81.0361280) >> >> x.temp<-c(-5,-5,-5,-5,-5,-5,-5,-5,-10,-10,-10,-10,-10,-10,-10, >> -10,-20,-20,-20,-20,-20,-20,-20,-20,-40,-40,-40,-40,-40, >> -40,-40,-40) >> >> nls(y.damage~a/x.temp+b,start=list(a=400,b=80)) >> plot(y.damage~x.temp,xlab='Temperature',ylab='Damage [%]') >> curve(409.61/x+81.84,from=min(x.temp),to=max(x.temp),add=T) > > A couple of comments. > > First, in general it is not straightforward to estimate > the value of a covariate (here temperature) by inverting > the regression of a response (here damage) on that covariate. > This the "inverse regression" or "calibration" problem, > (and it is problematic)! For instance, in linear regression > the estimate obtained by inversion has (theoretically) > no expectation, and has infinite variance. For an outline, > and a few references, see the Wikipedia article: > > http://en.wikipedia.org/wiki/Calibration_(statistics) > > Second, I would be inclined to try nls() on a reformulated > version of the problem. Let T50 denote the temperature for > 50% damage, and introduce this as a parameter (displacing > your parameter "a"): > > y = 50*(b + T50)/(b + x) > > where T50 = a/50 - b in terms of your original parameters > "a" and "b". With this formula for the non-linear dependence > of damage on temperature, it is no longer necessAry to invert > the regression equation, since the parameter you want is > already there and will be estimated directly. > > Hoping this helps, > Ted. I think I have mis-read your model: I read it as y = a/(x+b) whereas you wrote "y/x+b" and your model formula in nls() is y.damage~a/x.temp+b, i.e. y.damage ~ (a/x.temp) + b which confirms it. In that case, you may be able to get a satisfactory result by using a linear regression with y.damage = a*z.temp + b where z.temp = 1/x.temp so the model formula would be y.damage ~ z.temp You then have the straightforward inverse regression problem (aka calibration problem). The solution to this takes a bit of explanation, which I do not have the time for right now. I will write further about it in the morning. Ted. E-Mail: (Ted Harding) Fax-to-email: +44 (0)870 094 0861 Date: 11-Nov-11 Time: 23:07:35 --
[R] multcomp two-way anova with interactions within and between
Hi all, I'd like to compare all levels of my interaction with each other. I read the pdf 'Additional multcomp Examples' but even though there is an example with an interaction it doesn't work for me when I want to compare within and between groups. Here is an example: d.fr<-data.frame(id=rep(1:16,3),treat1=rep(as.factor(LETTERS[1:3]),each= 16),treat2=rep(as.factor(letters[4:7]),each=4),response=rnorm(48)) require(multcomp) require(lme4) fit1<-lmer(response~treat1*treat2+(1|id),data=d.fr) temp<-expand.grid(treat1=unique(d.fr$treat1),treat2=unique(d.fr$treat2)) X<-model.matrix(~treat1*treat2,data=temp) # X gives me a matrix with the dimensions 12 by 12. glht(fit1, linfct = X) Tukey<-contrMat(table(d.fr$treat1),'Tukey') K1<-cbind(Tukey,matrix(0,nrow=nrow(Tukey),ncol=ncol(Tukey))) rownames(K1) <- paste(levels(d.fr$treat2)[1],rownames(K1), sep = ":") K2 <- cbind(matrix(0, nrow = nrow(Tukey), ncol = ncol(Tukey)), Tukey) rownames(K2) <- paste(levels(d.fr$treat2)[2],rownames(K1), sep = ":") K3 <- cbind(matrix(0, nrow = nrow(Tukey), ncol = ncol(Tukey)), Tukey) rownames(K3) <- paste(levels(d.fr$treat2)[3],rownames(K1), sep = ":") K4 <- cbind(matrix(0, nrow = nrow(Tukey), ncol = ncol(Tukey)), Tukey) rownames(K4) <- paste(levels(d.fr$treat2)[4],rownames(K1), sep = ":") K <- rbind(K1, K2, K3,K4) colnames(K) <- c(colnames(Tukey), colnames(Tukey)) #K gives me a matrix with the dimension 12 by 6 and this will obviously not work in the next step summary(glht(fit2, linfct = K %*% X)) So, my questions is how would a 12 by 12 matrix for K look like, in order to do K %*% X correctly for all levels within and between Tukey adjusted. But since I am not in interested in every *single* comparison within and between, I was wondering to how to come up with a potential maximal but empty matrix and code the comparisons myself. Thanks for any hints! Stefan __ 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] work with a subset of the dataset
Or with what I just learned: subset<-[mydata$age %in% c(20:30),] Thanks for explaining Michael! Stefan -Original Message- From: r-help-boun...@r-project.org on behalf of Marc Schwartz Sent: Thu 1/12/2012 8:38 PM To: R. Michael Weylandt Cc: r-help@r-project.org; manu79 Subject: Re: [R] work with a subset of the dataset Presuming that 'DF' is the data frame, I am not sure what is wrong with NewDF <- subset(DF, (age >= 20) & (age <= 30)) presuming that 20 and 30 are to be included. ? Marc Schwartz On Jan 12, 2012, at 9:26 PM, R. Michael Weylandt wrote: > You can probably do it more easily with the subset() function but in > my experience that often leads to more problems than solutions: > perhaps try this. > > idx <- with(DATA, which(age > 20 & age < 30)) > DATA[idx, ] > > Michael > > On Thu, Jan 12, 2012 at 5:25 PM, manu79 wrote: >> Hello, >> I have a big dataset with many variables and I would like to consider only >> the rows in which there is a specific value of a variable. >> >> I make an example for explain what I mean: >> I have 5 variables describing a person: age, sex, weight, colour of hair, >> colour of eyes. >> I have 1000 rows (1000 persons) and I want to consider only the persons >> whose age is between 20 to 30. How can I do? >> >> Thank you very much >> M. __ 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] work with a subset of the dataset
But better without calling your new data frame "subset" since it's a function as well. -Original Message- From: r-help-boun...@r-project.org on behalf of Schreiber, Stefan Sent: Thu 1/12/2012 8:50 PM To: Marc Schwartz; R. Michael Weylandt Cc: r-help@r-project.org; manu79 Subject: Re: [R] work with a subset of the dataset Or with what I just learned: subset<-[mydata$age %in% c(20:30),] Thanks for explaining Michael! Stefan -Original Message- From: r-help-boun...@r-project.org on behalf of Marc Schwartz Sent: Thu 1/12/2012 8:38 PM To: R. Michael Weylandt Cc: r-help@r-project.org; manu79 Subject: Re: [R] work with a subset of the dataset Presuming that 'DF' is the data frame, I am not sure what is wrong with NewDF <- subset(DF, (age >= 20) & (age <= 30)) presuming that 20 and 30 are to be included. ? Marc Schwartz On Jan 12, 2012, at 9:26 PM, R. Michael Weylandt wrote: > You can probably do it more easily with the subset() function but in > my experience that often leads to more problems than solutions: > perhaps try this. > > idx <- with(DATA, which(age > 20 & age < 30)) > DATA[idx, ] > > Michael > > On Thu, Jan 12, 2012 at 5:25 PM, manu79 wrote: >> Hello, >> I have a big dataset with many variables and I would like to consider only >> the rows in which there is a specific value of a variable. >> >> I make an example for explain what I mean: >> I have 5 variables describing a person: age, sex, weight, colour of hair, >> colour of eyes. >> I have 1000 rows (1000 persons) and I want to consider only the persons >> whose age is between 20 to 30. How can I do? >> >> Thank you very much >> M. __ 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. [[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] work with a subset of the dataset
Thanks for the warning ! Better use Michael's or Marc's suggestion instead. Stefan -Original Message- From: R. Michael Weylandt [mailto:michael.weyla...@gmail.com] Sent: Thu 1/12/2012 9:05 PM To: Schreiber, Stefan Cc: Marc Schwartz; r-help@r-project.org; manu79 Subject: Re: [R] work with a subset of the dataset Be careful: I think that's only going to check exact equality: i.e., it won't find 20.5, but it also won't find 19.7 which you might get when you mean 20 due to floating point error. If the OP has non-integer data, this will cause trouble. Michael PS -- you also don't need the call to `c` -- there's nothing the 20:30 sequence is being combined with. On Thu, Jan 12, 2012 at 10:50 PM, Schreiber, Stefan wrote: > Or with what I just learned: > > subset<-[mydata$age %in% c(20:30),] > > Thanks for explaining Michael! > > Stefan > > > > > -Original Message- > From: r-help-boun...@r-project.org on behalf of Marc Schwartz > Sent: Thu 1/12/2012 8:38 PM > To: R. Michael Weylandt > Cc: r-help@r-project.org; manu79 > Subject: Re: [R] work with a subset of the dataset > > Presuming that 'DF' is the data frame, I am not sure what is wrong with > > NewDF <- subset(DF, (age >= 20) & (age <= 30)) > > presuming that 20 and 30 are to be included. > > ? > > Marc Schwartz > > On Jan 12, 2012, at 9:26 PM, R. Michael Weylandt wrote: > >> You can probably do it more easily with the subset() function but in >> my experience that often leads to more problems than solutions: >> perhaps try this. >> >> idx <- with(DATA, which(age > 20 & age < 30)) >> DATA[idx, ] >> >> Michael >> >> On Thu, Jan 12, 2012 at 5:25 PM, manu79 wrote: >>> Hello, >>> I have a big dataset with many variables and I would like to consider >>> only >>> the rows in which there is a specific value of a variable. >>> >>> I make an example for explain what I mean: >>> I have 5 variables describing a person: age, sex, weight, colour of hair, >>> colour of eyes. >>> I have 1000 rows (1000 persons) and I want to consider only the persons >>> whose age is between 20 to 30. How can I do? >>> >>> Thank you very much >>> M. > > __ > 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.
[R] for loop problem
Hi all, I was wondering if you can help me with the following situation: I have a data frame that includes weather station data for 30 years in the form: YEAR, MONTH, DAY, TEMP 1970, 01, 01, -15 ... 1999, 12, 31, -21 I would like to add another variable "JULIAN" that assigns the integers 1 to 365 (and 1 to 366 for leap years) for each day of a year over multiple years. Here is what I came up with: counter<-1 for(i in 1:12){ for (j in 1:31){ df$JULIAN[df$MONTH==i & stn$DAY==j]<- counter counter<-counter+1 } } R does it exactly what I told it to but I am not satisfied with it since it doesn't stop assigning the integers when months have 28, 29 or 30 days. For instance on Feb-28 JULIAN is 59 and on March-1 it's 64, as opposed to be 60. I am assuming it must be an ifelse statement, and I was messing around with it already but without success. I guess I am missing some vocabulary here and hope someone can give me some pointers. Thanks, Stefan [[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] for loop problem
Hey Michael and Tyler! Thanks for the input and the effort! The lubridate package is really awesome! What I did now is just this here: dat$DATE<-as.Date(paste(dat$YEAR,dat$MONTH,dat$DAY,sep='-'),format='%Y-%m-%d') require(lubridate) dat$JULIAN<-yday(dat$DATE) That's it. Nice and easy :) Thanks! Stefan -Original Message- From: Tyler Rinker [mailto:tyler_rin...@hotmail.com] Sent: Mon 4/30/2012 6:40 PM To: Schreiber, Stefan; r-help@r-project.org Subject: RE: [R] for loop problem I don't really work with dates but thought I'd pass a solution on. I think that there some great packages for handling dates though (lubridate) and you may want to convert your data to a true date instead of separate columns. # FUNCTION TO INDEX DATES date.int <- function(month, year, day){ yearLength <- function(year) year %% 4 == 0 key1 <- data.frame(m=1:12, nm=c(31, 28, 31, 30, 31, 30, 31, 30, 31, 31, 30, 31)) key1$sum <- c(0, cumsum(key1$nm)[-12]) key2 <- data.frame(m=1:12, nm=c(31, 29, 31, 30, 31, 30, 31, 30, 31, 31, 30, 31)) key2$sum <- c(0, cumsum(key2$nm)[-12]) ifelse(yearLength(year), key1[month-1, 3] + day, key2[month-1, 3] + day) } #TRY IT OUT ON A MADE UP DATA SET M <- sort(sample(seq(as.Date("2000/1/1"), by="day", length.out=1000), 10)) N <- do.call(rbind, strsplit(as.character(M), "\\-")) N <- data.frame(apply(N, 2, as.numeric)) colnames(N) <- c('year', 'month', 'day') with(N, date.int(month=month, year=year, day=day)) > Date: Mon, 30 Apr 2012 17:57:02 -0600 > From: stefan.schrei...@ales.ualberta.ca > To: r-help@r-project.org > Subject: [R] for loop problem > > Hi all, > > I was wondering if you can help me with the following situation: > > I have a data frame that includes weather station data for 30 years in > the form: > > YEAR, MONTH, DAY, TEMP > 1970, 01, 01, -15 > ... > 1999, 12, 31, -21 > > I would like to add another variable "JULIAN" that assigns the integers > 1 to 365 (and 1 to 366 for leap years) for each day of a year over > multiple years. > > Here is what I came up with: > > counter<-1 > > for(i in 1:12){ > for (j in 1:31){ > > df$JULIAN[df$MONTH==i & stn$DAY==j]<- counter > counter<-counter+1 > } > } > > R does it exactly what I told it to but I am not satisfied with it since > it doesn't stop assigning the integers when months have 28, 29 or 30 > days. For instance on Feb-28 JULIAN is 59 and on March-1 it's 64, as > opposed to be 60. > > I am assuming it must be an ifelse statement, and I was messing around > with it already but without success. > > I guess I am missing some vocabulary here and hope someone can give me > some pointers. > > Thanks, > Stefan > > [[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.
[R] Why horizontal bars instead of a line
Hey list! It looks simple, though it's not possible for me to plot the following properly: (some made-up data) GrSeClone1 Clone2 Clone3 Clone4 Clone... G1999 2 3 6 5 G2000 2 5 7 4 G2001 5 3 7 3 G2002 4 5 8 3 G... GrSe=Growing Season. I've read the file as "x" and then tried:plot(x$GrSe,x$Clone1) The output is 4 horizontal bars. Even if I write plot(x$GrSe,x$Clone1,type="n") R is still plotting! I figured already that if I delete the "G" in front of the years it'll work, however, I'd like to keep the "G". Is that possible? Hope it is an appropriate question for the list. Thanks, Stefan [[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] Why horizontal bars instead of a line
Hey Greg! Thank you very much for your detailed response!! I really appreciate that! Yeah, you're right! The axis labels are the way to go ... I was so focused on getting rid of the bars, that I didn't even think about that. Sorry for bothering the list about that! Stefan -Original Message- From: Greg Snow [mailto:[EMAIL PROTECTED] Sent: Wednesday, September 24, 2008 12:06 PM To: Schreiber, Stefan; r-help@r-project.org Subject: RE: Why horizontal bars instead of a line Remember that computers are not as smart as you. Some smart people have written instructions for the computer on what to do in certain cases, but they can't anticipate everything, so when you tell the computer to do something that was different from what is anticipated, it either gives an error or the results of a wrong guess. In your case, since GrSe has a G in the front and your data is probably in a data frame, the plot function sees a factor (generally what gets created automatically when characters are seen) and passes the data to plot.factor which plots boxplots (and with 1 point per year, the 5 number summary for the boxplot gives all the same value, hence the horizontal line). What you probably want to do is to plot your clone data against a numerical representation of the year (without the G), but suppress the axis labels and add the labels yourself (with the G), see ?axis for detail on doing this. Hope this helps, -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare [EMAIL PROTECTED] 801.408.8111 > -Original Message- > From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] > project.org] On Behalf Of Schreiber, Stefan > Sent: Wednesday, September 24, 2008 9:28 AM > To: r-help@r-project.org > Subject: [R] Why horizontal bars instead of a line > > Hey list! > > > It looks simple, though it's not possible for me to plot the following > properly: > > (some made-up data) > > GrSeClone1 Clone2 Clone3 Clone4 Clone... > G1999 2 3 6 5 > G2000 2 5 7 4 > G2001 5 3 7 3 > G2002 4 5 8 3 > G... > > > > GrSe=Growing Season. > > I've read the file as "x" and then tried:plot(x$GrSe,x$Clone1) > > The output is 4 horizontal bars. Even if I write > plot(x$GrSe,x$Clone1,type="n") R is still plotting! > I figured already that if I delete the "G" in front of the years it'll > work, however, I'd like to keep the "G". > Is that possible? > > Hope it is an appropriate question for the list. > > Thanks, > Stefan > > > > > [[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-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] shifting ticks to left or right
Hey list, Does anybody knows a command to centre the tick mark labels exactly between the tick points (right shift)? And then to exclude the last tick label on the right? I know one can shift them using the 'hadj' option in par. But I am wondering if there is a more convenient command! Thanks a lot!! Stefan [[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] shifting ticks to left or right
Thank you very much! That's the way to go! Cheers, Stefan -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] Sent: Wednesday, October 01, 2008 10:52 AM To: Schreiber, Stefan Cc: r-help@r-project.org; [EMAIL PROTECTED] Subject: Re: [R] shifting ticks to left or right > Does anybody knows a command to centre the tick mark labels exactly > between the tick points (right shift)? > And then to exclude the last tick label on the right? > > I know one can shift them using the 'hadj' option in par. But I am > wondering if there is a more convenient command! A little nasty, but: plot(1:10, axes=FALSE) par(tcl=-0.5) Axis(side=1, at=c(1,5,9), labels=rep("",3)) par(tcl=0) Axis(side=1, at=c(3,7), labels=c("foo", "bar")) Regards, Richie. Mathematical Sciences Unit HSL Schreiber, Stefan wrote: > Hey list, > > Does anybody knows a command to centre the tick mark labels exactly > between the tick points (right shift)? > And then to exclude the last tick label on the right? > > I know one can shift them using the 'hadj' option in par. But I am > wondering if there is a more convenient command! > > Thanks a lot!! Plot the axes twice: once with ticks and no labels, once with labels and no ticks, e.g. plot(1:10, axes=F) axis(1, labels=F) axis(1, at = c(1,3,5,7,9), lty=0) Duncan Murdoch -Original Message- From: Peter Dalgaard [mailto:[EMAIL PROTECTED] Sent: Wednesday, October 01, 2008 10:38 AM To: Schreiber, Stefan Cc: r-help@r-project.org Subject: Re: [R] shifting ticks to left or right Schreiber, Stefan wrote: > Hey list, > > Does anybody knows a command to centre the tick mark labels exactly > between the tick points (right shift)? > And then to exclude the last tick label on the right? > > I know one can shift them using the 'hadj' option in par. But I am > wondering if there is a more convenient command! > > Thanks a lot!! > > Stefan > Hmm, are you solving the right problem there? Sounds like it would be easier to do TWO axes, one with no labels and another with no tickmarks: z<-seq(from=.5,length=10) plot(z,sin(z),xaxt="n", xlim=c(0,10)) axis(1,at=z, labels=letters[1:10], tick=F) axis(1,at=0:10, labels=F) -- O__ Peter Dalgaard Øster Farimagsgade 5, Entr.B c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~ - ([EMAIL PROTECTED]) FAX: (+45) 35327907 __ 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] combining bar and line plots with multiple axes
Hey list, I have a barplot with a line plot overlayed (on the 2nd y axis) and it looks fine except that the origin (0) on the 2nd y axis (side=4) is not lining up with the origin on the 1st y-axis (side=2, from the barplot). Is there a way to either shift the 2nd y-axis (line plot) down or readjust the barplot axis? Just as a reference, here is the code: barplot2(temp$precip, names.arg=temp$year, ylim=c(0,300),axes=F) axis(2) par(new=T) plot(temp$year, temp$mat, axes=F, type="l", ylim=(0,25)) axis(4) Thanks!! Stefan [[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] save single image when using plot(lm(y~x))
Dear List! When I'm using "plot(lm(y~x))" R shows me 4 different plots. They pop-up individually by clicking on the graph. How can I save for example the 2nd plot? I've found a camera button that says "copy to the clipboard as a metafile" once you place the mouse pointer on it. I tried, though it's not working. Any other suggestions? Thanks!! Stefan [[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] save single image when using plot(lm(y~x))
Woohoo! Thanks a lot Juliet! Stefan -Original Message- From: Juliet Hannah [mailto:[EMAIL PROTECTED] Sent: Friday, November 28, 2008 2:42 PM To: Schreiber, Stefan Cc: r-help@r-project.org Subject: Re: [R] save single image when using plot(lm(y~x)) Try: mylm <- plot(lm(y~x)) plot(mylm,which=2) On Fri, Nov 28, 2008 at 4:12 PM, Schreiber, Stefan <[EMAIL PROTECTED]> wrote: > Dear List! > > When I'm using "plot(lm(y~x))" R shows me 4 different plots. They pop-up > individually by clicking on the graph. How can I save for example the > 2nd plot? I've found a camera button that says "copy to the clipboard > as a metafile" once you place the mouse pointer on it. I tried, though > it's not working. > > Any other suggestions? > > Thanks!! > > Stefan > >[[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-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] save single image when using plot(lm(y~x))
Thanks Paul!! I wasn't aware of the command "plot.lm". Therefore, I couldn't find the help-file either. Cheers, Stefan -Original Message- From: Paul Hiemstra [mailto:[EMAIL PROTECTED] Sent: Friday, November 28, 2008 5:10 PM To: Juliet Hannah Cc: Schreiber, Stefan; r-help@r-project.org Subject: Re: [R] save single image when using plot(lm(y~x)) Hi, The plot function in R is a so called generic function, an appropriate function is chosen based on the input object provided by the user. In this case an lm-object (use the class() command to find this). The plot function that R calls is then called plot.lm. ?plot.lm gives you the help file and the first sentence in the help file already answers your problem. hope this helps, Paul Juliet Hannah schreef: > Try: > mylm <- plot(lm(y~x)) > plot(mylm,which=2) > > > > On Fri, Nov 28, 2008 at 4:12 PM, Schreiber, Stefan > <[EMAIL PROTECTED]> wrote: > >> Dear List! >> >> When I'm using "plot(lm(y~x))" R shows me 4 different plots. They pop-up >> individually by clicking on the graph. How can I save for example the >> 2nd plot? I've found a camera button that says "copy to the clipboard >> as a metafile" once you place the mouse pointer on it. I tried, though >> it's not working. >> >> Any other suggestions? >> >> Thanks!! >> >> Stefan >> >>[[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-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. > -- Drs. Paul Hiemstra Department of Physical Geography Faculty of Geosciences University of Utrecht Heidelberglaan 2 P.O. Box 80.115 3508 TC Utrecht Phone: +31302535773 Fax:+31302531145 http://intamap.geo.uu.nl/~paul __ 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.