Re: [R] sort by column and row names
Yes, that is it. Thank you very much, Phil. -Original Message- From: Phil Spector [mailto:spec...@stat.berkeley.edu] Sent: Thursday, February 17, 2011 3:36 PM To: Jim Moon Cc: r-help@r-project.org Subject: Re: [R] sort by column and row names Jim - I believe dat[order(rownames(dat)),order(colnames(dat))] is what you are looking for. - Phil Spector Statistical Computing Facility Department of Statistics UC Berkeley spec...@stat.berkeley.edu On Thu, 17 Feb 2011, Jim Moon wrote: > Hello, All, > > How can one sort on column and row names. For example: > > How can this > X1 X3 X2 > X1 1 0 0 > X3 0 1 0 > X2 0 0 1 > > become this? > X1 X2 X3 > X1 1 0 0 > X2 0 1 0 > X3 0 0 1 > > > Thank you for your time! > > Jim > > > [[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] sort by column and row names
Hello, All, How can one sort on column and row names. For example: How can this X1 X3 X2 X1 1 0 0 X3 0 1 0 X2 0 0 1 become this? X1 X2 X3 X1 1 0 0 X2 0 1 0 X3 0 0 1 Thank you for your time! Jim [[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] GWAF package: lme.batch.imputed(): object 'kmat' not found
Hello, All, GWAF 1.2 R.Version() is below. system(lme.batch.imputed( phenfile = 'phenfile.csv', genfile = 'CARe_imputed_release.0.fhsR.gz', pedfile='pedfile.csv', phen='phen1', covar=c('covar1','covar2'), kinmat='imputed_fhs.kinship.RData', outfile='imputed.FHS.IBC.GWAF.LME.output.0.txt' )) Gives the error messages: Error in coxme.varcheck(ncluster, varlist, n, gvars, groups, sparse, rescale, : object 'kmat' not found Error in lme.cov.out$theta : $ operator is invalid for atomic vectors Might anyone know why? Thank you for your time! Jim --- > R.Version() $platform [1] "x86_64-apple-darwin9.6.0" $arch [1] "x86_64" $os [1] "darwin9.6.0" $system [1] "x86_64, darwin9.6.0" $status [1] "" $major [1] "2" $minor [1] "12.1" $year [1] "2010" $month [1] "12" $day [1] "16" $`svn rev` [1] "53855" $language [1] "R" $version.string [1] "R version 2.12.1 (2010-12-16)" [[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] write.table -- maintain decimal places
Great. Thank you, Peter! -Original Message- From: Peter Ehlers [mailto:ehl...@ucalgary.ca] Sent: Tuesday, January 25, 2011 7:26 PM To: Jim Moon Cc: r-help@r-project.org Subject: Re: [R] write.table -- maintain decimal places On 2011-01-25 17:22, Jim Moon wrote: > Thank you for the response, Peter. > > The approach: > write.table(format(df, > drop0trailing=FALSE),file='df.txt',quote=F,sep='\t',row.names=F) > surprisingly still results in some loss of trailing 0's. Here are a couple more (essentially identical) ways: # 1. dfm <- within(df, { EFFECT2 <- sprintf("%6.3f", EFFECT2) PVALUE <- sprintf("%7.5f", PVALUE) }) # 2. dfm <- within(df, { EFFECT2 <- formatC(EFFECT2, format="f", digits=3) PVALUE <- formatC(PVALUE, format="f", digits=5) }) write.table(dfm, file='dfm.txt', quote=FALSE, sep='\t', row.names=FALSE) Peter Ehlers > > df: > EFFECT2 PVALUE > 1 0.0230.88080 > 2 -0.260 0.08641 > 3 -0.114 0.45200 > > df.txt: > EFFECT2PVALUE > 0.023 8.808e-01 > -0.26 8.641e-02 > -0.114 4.520e-01 > > > -Original Message- > From: Peter Ehlers [mailto:ehl...@ucalgary.ca] > Sent: Tuesday, January 25, 2011 5:09 PM > To: Jim Moon > Cc: r-help@r-project.org > Subject: Re: [R] write.table -- maintain decimal places > > On 2011-01-25 16:16, Jim Moon wrote: >> Hello, All, >> >> How can I maintain the decimal places when using write.table()? >> >> Jim >> >> e.g. >> >> df: >> EFFECT2 PVALUE >> 1 0.0230.88080 >> 2 -0.260 0.08641 >> 3 -0.114 0.45200 >> >> write.table(df,file='df.txt',quote=F,sep='\t',row.names=F) > >write.table(format(df, drop0trailing=FALSE), ) > > Peter Ehlers > >> >> >> df.txt: >> EFFECT2PVALUE >> 0.023 0.8808 >> -0.26 0.08641 >> -0.114 0.452 >> >> [[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] write.table -- maintain decimal places
I am using: "R version 2.11.1 (2010-05-31)" It is good to know that it works in 2.12.1 Jim -Original Message- From: Peter Ehlers [mailto:ehl...@ucalgary.ca] Sent: Tuesday, January 25, 2011 5:57 PM To: Jim Moon Cc: r-help@r-project.org Subject: Re: [R] write.table -- maintain decimal places On 2011-01-25 17:22, Jim Moon wrote: > Thank you for the response, Peter. > > The approach: > write.table(format(df, > drop0trailing=FALSE),file='df.txt',quote=F,sep='\t',row.names=F) > surprisingly still results in some loss of trailing 0's. > What version of R? I'm using R version 2.12.1 Patched (2010-12-27 r53883) and it works for me. Peter Ehlers > df: > EFFECT2 PVALUE > 1 0.0230.88080 > 2 -0.260 0.08641 > 3 -0.114 0.45200 > > df.txt: > EFFECT2PVALUE > 0.023 8.808e-01 > -0.26 8.641e-02 > -0.114 4.520e-01 > > > -Original Message----- > From: Peter Ehlers [mailto:ehl...@ucalgary.ca] > Sent: Tuesday, January 25, 2011 5:09 PM > To: Jim Moon > Cc: r-help@r-project.org > Subject: Re: [R] write.table -- maintain decimal places > > On 2011-01-25 16:16, Jim Moon wrote: >> Hello, All, >> >> How can I maintain the decimal places when using write.table()? >> >> Jim >> >> e.g. >> >> df: >> EFFECT2 PVALUE >> 1 0.0230.88080 >> 2 -0.260 0.08641 >> 3 -0.114 0.45200 >> >> write.table(df,file='df.txt',quote=F,sep='\t',row.names=F) > >write.table(format(df, drop0trailing=FALSE), ) > > Peter Ehlers > >> >> >> df.txt: >> EFFECT2PVALUE >> 0.023 0.8808 >> -0.26 0.08641 >> -0.114 0.452 >> >> [[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] write.table -- maintain decimal places
Thank you for the response, Peter. The approach: write.table(format(df, drop0trailing=FALSE),file='df.txt',quote=F,sep='\t',row.names=F) surprisingly still results in some loss of trailing 0's. df: EFFECT2 PVALUE 1 0.0230.88080 2 -0.260 0.08641 3 -0.114 0.45200 df.txt: EFFECT2PVALUE 0.023 8.808e-01 -0.26 8.641e-02 -0.114 4.520e-01 -Original Message- From: Peter Ehlers [mailto:ehl...@ucalgary.ca] Sent: Tuesday, January 25, 2011 5:09 PM To: Jim Moon Cc: r-help@r-project.org Subject: Re: [R] write.table -- maintain decimal places On 2011-01-25 16:16, Jim Moon wrote: > Hello, All, > > How can I maintain the decimal places when using write.table()? > > Jim > > e.g. > > df: >EFFECT2 PVALUE > 1 0.0230.88080 > 2 -0.260 0.08641 > 3 -0.114 0.45200 > > write.table(df,file='df.txt',quote=F,sep='\t',row.names=F) write.table(format(df, drop0trailing=FALSE), ) Peter Ehlers > > > df.txt: > EFFECT2PVALUE > 0.023 0.8808 > -0.26 0.08641 > -0.114 0.452 > > [[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] write.table -- maintain decimal places
Hello, All, How can I maintain the decimal places when using write.table()? Jim e.g. df: EFFECT2 PVALUE 1 0.0230.88080 2 -0.260 0.08641 3 -0.114 0.45200 write.table(df,file='df.txt',quote=F,sep='\t',row.names=F) df.txt: EFFECT2PVALUE 0.023 0.8808 -0.26 0.08641 -0.114 0.452 [[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] read in data, maintain decimal places
David, Perfect. Thank you! Jim -Original Message- From: David Winsemius [mailto:dwinsem...@comcast.net] Sent: Friday, January 14, 2011 2:26 PM To: Jim Moon Cc: r-help@r-project.org Subject: Re: [R] read in data, maintain decimal places On Jan 14, 2011, at 5:22 PM, Jim Moon wrote: > Good day, All, > > Is there any way to maintain the number of decimal places in the > type of situation below? > I would like to maintain the number of decimal places in 0.667, > despite the fact that its column-mates have a fourth decimal place. You would need to read it as character. If that is your choice then use the colClasses argument. > > Thank you for your time. > > Jim > > dat.txt contents: > > MARKER ALLELESFREQ1 RSQR EFFECT2STDERR > CHISQ PVALUE > rs6599753 C,T .2087 .1866 0.166 0.387 > 0.1850 0.6671 > rs12443141 A,T .2087 .1868 0.166 0.386 > 0.1851 0.667 > rs12905389 A,G .2806 .6149 -0.038 0.193 > 0.0381 0.8452 > > R: >> dat = read.delim( dat.txt) >> dat > MARKER ALLELES FREQ1 RSQR EFFECT2 STDERR CHISQ PVALUE > 1 rs6599753 C,T 0.2087 0.1866 0.166 0.387 0.1850 0.6671 > 2 rs12443141 A,T 0.2087 0.1868 0.166 0.386 0.1851 0.6670 > 3 rs12905389 A,G 0.2806 0.6149 -0.038 0.193 0.0381 0.8452 > -- David Winsemius, MD West Hartford, CT __ 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] read in data, maintain decimal places
Good day, All, Is there any way to maintain the number of decimal places in the type of situation below? I would like to maintain the number of decimal places in 0.667, despite the fact that its column-mates have a fourth decimal place. Thank you for your time. Jim dat.txt contents: MARKER ALLELESFREQ1 RSQR EFFECT2STDERR CHISQ PVALUE rs6599753 C,T .2087 .1866 0.166 0.387 0.1850 0.6671 rs12443141 A,T .2087 .1868 0.166 0.386 0.1851 0.667 rs12905389 A,G .2806 .6149 -0.038 0.193 0.0381 0.8452 R: >dat = read.delim( dat.txt) >dat MARKER ALLELES FREQ1 RSQR EFFECT2 STDERR CHISQ PVALUE 1 rs6599753 C,T 0.2087 0.1866 0.166 0.387 0.1850 0.6671 2 rs12443141 A,T 0.2087 0.1868 0.166 0.386 0.1851 0.6670 3 rs12905389 A,G 0.2806 0.6149 -0.038 0.193 0.0381 0.8452 [[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] with(data.frame,ifelse(___,___,___))
Hello, All, Mac OS 10.6.5 R64 2.11.1 This works as expected: f1 = c(0.084, 0.099, 0) data= data.frame(f1) data$f1=with(data,ifelse(f1==0, 0.0001, f1)) data f1 1 0.0840 2 0.0990 3 0.0001 Substituting 'f1==0' with 'T' produces the expected result: f1 = c(0.084, 0.099, 0) data= data.frame(f1) data$f1=with(data,ifelse(T, 0.0001, f1)) data f1 1 1e-04 2 1e-04 3 1e-04 But, substituting 'f1==0' or 'T' with 'F' does not produce the expected result: f1 = c(0.084, 0.099, 0) data= data.frame(f1) data$f1=with(data,ifelse(F, 0.0001, f1)) data f1 1 0.084 2 0.084 3 0.084 I would expect this: f1 1 0.084 2 0.099 3 0 Is with(data.frame,ifelse(___,___,___)) broken, or does my interpretation of it need fixing? Thank you for your time. Jim [[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] Write.table eol argument
Hello All, R 2.11.1 Windows XP, 32-bit Help says that default is eol='\n'. To me, that represents Linefeed (LF) >From Help: eol the character(s) to print at the end of each line (row). For example, eol="\r\n" will produce Windows' line endings on a Unix-alike OS, and eol="\r" will produce files as expected by Mac OS Excel 2004. I would like for write.table to end each line with LF only-no carriage return (CR). Default eol='\n' generates CRLF Explicit eol='\n' generates CRLF eol='\r' generates CR eol='\r\n' generates (predictably) CRCRLF Thank you for your time. Jim [[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] sd() for numeric row entries
This is very helpful, Phil. Being new to R and trying to get a handle on *apply, by, aggregate... this is great. Thank you. Jim -Original Message- From: Phil Spector [mailto:spec...@stat.berkeley.edu] Sent: Wednesday, December 08, 2010 2:36 PM To: Jim Moon Cc: r-help@r-project.org Subject: Re: [R] sd() for numeric row entries Jim - Suppose the data frame is named "df". Then df$std_dev = apply(df,1,sd,na.rm=TRUE) will do what you want, although it will generate some warning messages due to the first column. df$sd_dev = apply(df[,sapply(df,is.numeric)],1,sd) does the same, but won't produce warnings. - Phil Spector Statistical Computing Facility Department of Statistics UC Berkeley spec...@stat.berkeley.edu On Wed, 8 Dec 2010, Jim Moon wrote: > How might one calculate standard deviation, row-wise, for the numeric values > in a data frame such as this one > V1 V2 V3 V4 V5 > 1 rs11089130 0.4565 0.4574 0.4569 0.4572 > 2 rs738829 0.6548 0.6519 0.6448 0.6549 > 3 rs915674 0.7503 0.7500 0.7517 0.7502 > > and place the standard deviation in a 6th column called ???std_dev > > Regards, > Jim > > > [[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] sd() for numeric row entries
How might one calculate standard deviation, row-wise, for the numeric values in a data frame such as this one V1 V2 V3 V4 V5 1 rs11089130 0.4565 0.4574 0.4569 0.4572 2 rs738829 0.6548 0.6519 0.6448 0.6549 3 rs915674 0.7503 0.7500 0.7517 0.7502 and place the standard deviation in a 6th column called âstd_devâ? Regards, Jim [[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] FW: how to use by() ?
Well-phrased, David. :-) Jim -Original Message- From: David Winsemius [mailto:dwinsem...@comcast.net] Sent: Monday, November 29, 2010 10:53 AM To: Jim Moon Cc: r-help@r-project.org Subject: Re: [R] FW: how to use by() ? On Nov 29, 2010, at 1:36 PM, Jim Moon wrote: > Thank you for the suggestion, Bill. The result is not quite what I > would like. Here's sample code for you or anyone else who may be > interested: > > Al1 = c('A','C','C','C') > Al2 = c('G','G','G','T') > Freq1 = c(0.0078,0.0567,0.9434,0.9908) > MAF = c(0.0078,0.0567,0.0566,0.0092) > m1 = data.frame(Al1=Al1, Al2=Al2,Freq1=Freq1,MAF=MAF,major_allele='') > m1 > > Al1 Al2 Freq1MAF major_allele > 1 A G 0.0078 0.0078 > 2 C G 0.0567 0.0567 > 3 C G 0.9434 0.0566 > 4 C T 0.9908 0.0092 > > > Using the suggestion involving "with()" (I swapped Al1 and Al2 from > before, but this does not affect the nature of the output): > > m1$major_allele <- with(m1, ifelse(Freq1==MAF, Al2, Al1));m1 > > Al1 Al2 Freq1MAF major_allele I suspect that you have just been bitten by the data.frame- stringsAsFactors=TRUE crocodile. Since you are comparing floating point numbers, you are also wading in rivers where floating-point crocodiles are also hungry and searching out their next victim. -- David. > 1 A G 0.0078 0.00781 > 2 C G 0.0567 0.05671 > 3 C G 0.9434 0.05662 > 4 C T 0.9908 0.00922 > > > The output I desire is: > Al1 Al2 Freq1MAF major_allele > 1 A G 0.0078 0.0078G > 2 C G 0.0567 0.0567G > 3 C G 0.9434 0.0566C > 4 C T 0.9908 0.0092C > > > Jim > > > -Original Message- > From: William Dunlap [mailto:wdun...@tibco.com] > Sent: Monday, November 29, 2010 10:02 AM > To: Jim Moon > Subject: RE: [R] how to use by() ? > > m1$major_allele <- with(m1, ifelse(Freq1==MAF, Al1, Al2)) > > 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 Jim Moon >> Sent: Monday, November 29, 2010 9:44 AM >> To: r-help@r-project.org >> Subject: [R] how to use by() ? >> >> Hello, All! >> >> How might one accomplish this using the by() function? >> m1 is a data frame. >> >> # populate column "m1$major_allele" >> for ( i in 1:length(m1$major_allele)) { >> if ( m1$Freq1[i] == m1$MAF[i]){ >>m1$major_allele[i] = m1$Al1[i] >> } >> else{ >> m1$major_allele[i] = m1$Al2[i] >> } >> } >> >> >> Jim >> >> [[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. David Winsemius, MD West Hartford, CT __ 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] FW: how to use by() ?
Thank you, Bill. That fixed it. Jim -Original Message- From: William Dunlap [mailto:wdun...@tibco.com] Sent: Monday, November 29, 2010 10:46 AM To: Jim Moon; r-help@r-project.org Subject: RE: [R] FW: how to use by() ? ifelse(cond,ifTrue,ifFalse) doesn't do what you want when ifTrue or ifElse is a factor. You can use as.character on the factors > with(m1, ifelse(Freq1==MAF, as.character(Al2), as.character(Al1))) [1] "G" "G" "C" "C" or use the stringsAsFactors=FALSE argument to data.frame (or read.table) when you make the data.frame. 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 Jim Moon > Sent: Monday, November 29, 2010 10:37 AM > To: r-help@r-project.org > Subject: [R] FW: how to use by() ? > > Thank you for the suggestion, Bill. The result is not quite > what I would like. Here's sample code for you or anyone else > who may be interested: > > Al1 = c('A','C','C','C') > Al2 = c('G','G','G','T') > Freq1 = c(0.0078,0.0567,0.9434,0.9908) > MAF = c(0.0078,0.0567,0.0566,0.0092) > m1 = data.frame(Al1=Al1, > Al2=Al2,Freq1=Freq1,MAF=MAF,major_allele='') > m1 > > Al1 Al2 Freq1MAF major_allele > 1 A G 0.0078 0.0078 > 2 C G 0.0567 0.0567 > 3 C G 0.9434 0.0566 > 4 C T 0.9908 0.0092 > > > Using the suggestion involving "with()" (I swapped Al1 and > Al2 from before, but this does not affect the nature of the output): > > m1$major_allele <- with(m1, ifelse(Freq1==MAF, Al2, Al1));m1 > > Al1 Al2 Freq1MAF major_allele > 1 A G 0.0078 0.00781 > 2 C G 0.0567 0.05671 > 3 C G 0.9434 0.05662 > 4 C T 0.9908 0.00922 > > > The output I desire is: > Al1 Al2 Freq1MAF major_allele > 1 A G 0.0078 0.0078 G > 2 C G 0.0567 0.0567G > 3 C G 0.9434 0.0566C > 4 C T 0.9908 0.0092C > > > Jim > > > -Original Message- > From: William Dunlap [mailto:wdun...@tibco.com] > Sent: Monday, November 29, 2010 10:02 AM > To: Jim Moon > Subject: RE: [R] how to use by() ? > > m1$major_allele <- with(m1, ifelse(Freq1==MAF, Al1, Al2)) > > 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 Jim Moon > > Sent: Monday, November 29, 2010 9:44 AM > > To: r-help@r-project.org > > Subject: [R] how to use by() ? > > > > Hello, All! > > > > How might one accomplish this using the by() function? > > m1 is a data frame. > > > > # populate column "m1$major_allele" > > for ( i in 1:length(m1$major_allele)) { > > if ( m1$Freq1[i] == m1$MAF[i]){ > > m1$major_allele[i] = m1$Al1[i] > > } > > else{ > > m1$major_allele[i] = m1$Al2[i] > > } > > } > > > > > > Jim > > > > [[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-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] how to use by() ?
Jim Moon ohsu.edu> writes: > How might one accomplish this using the by() function? > m1 is a data frame. > > # populate column "m1$major_allele" > for ( i in 1:length(m1$major_allele)) { > if ( m1$Freq1[i] == m1$MAF[i]){ > m1$major_allele[i] = m1$Al1[i] > } > else{ > m1$major_allele[i] = m1$Al2[i] > } > } You could use: m1$major_allele <- ifelse( m1$Freq1 == m1$MAF, m1$Al1, m1$Al2 ) Greg --- Thank you for the suggestion, Greg. The result is not quite what I would like. Here's sample code for you or anyone else who may be interested: Al1 = c('A','C','C','C') Al2 = c('G','G','G','T') Freq1 = c(0.0078,0.0567,0.9434,0.9908) MAF = c(0.0078,0.0567,0.0566,0.0092) m1 = data.frame(Al1=Al1, Al2=Al2,Freq1=Freq1,MAF=MAF,major_allele='') m1 Al1 Al2 Freq1MAF major_allele 1 A G 0.0078 0.0078 2 C G 0.0567 0.0567 3 C G 0.9434 0.0566 4 C T 0.9908 0.0092 Using the suggestion involving "ifelse" (I swapped Al1 and Al2 from before, but this does not affect the nature of the output): m1$major_allele <- ifelse( m1$Freq1 == m1$MAF, m1$Al2, m1$Al1 );m1 Al1 Al2 Freq1MAF major_allele 1 A G 0.0078 0.00781 2 C G 0.0567 0.05671 3 C G 0.9434 0.05662 4 C T 0.9908 0.00922 The output I desire is: Al1 Al2 Freq1MAF major_allele 1 A G 0.0078 0.0078G 2 C G 0.0567 0.0567G 3 C G 0.9434 0.0566C 4 C T 0.9908 0.0092C Jim [[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] FW: how to use by() ?
Thank you for the suggestion, Bill. The result is not quite what I would like. Here's sample code for you or anyone else who may be interested: Al1 = c('A','C','C','C') Al2 = c('G','G','G','T') Freq1 = c(0.0078,0.0567,0.9434,0.9908) MAF = c(0.0078,0.0567,0.0566,0.0092) m1 = data.frame(Al1=Al1, Al2=Al2,Freq1=Freq1,MAF=MAF,major_allele='') m1 Al1 Al2 Freq1MAF major_allele 1 A G 0.0078 0.0078 2 C G 0.0567 0.0567 3 C G 0.9434 0.0566 4 C T 0.9908 0.0092 Using the suggestion involving "with()" (I swapped Al1 and Al2 from before, but this does not affect the nature of the output): m1$major_allele <- with(m1, ifelse(Freq1==MAF, Al2, Al1));m1 Al1 Al2 Freq1MAF major_allele 1 A G 0.0078 0.00781 2 C G 0.0567 0.05671 3 C G 0.9434 0.05662 4 C T 0.9908 0.00922 The output I desire is: Al1 Al2 Freq1MAF major_allele 1 A G 0.0078 0.0078G 2 C G 0.0567 0.0567G 3 C G 0.9434 0.0566C 4 C T 0.9908 0.0092C Jim -Original Message- From: William Dunlap [mailto:wdun...@tibco.com] Sent: Monday, November 29, 2010 10:02 AM To: Jim Moon Subject: RE: [R] how to use by() ? m1$major_allele <- with(m1, ifelse(Freq1==MAF, Al1, Al2)) 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 Jim Moon > Sent: Monday, November 29, 2010 9:44 AM > To: r-help@r-project.org > Subject: [R] how to use by() ? > > Hello, All! > > How might one accomplish this using the by() function? > m1 is a data frame. > > # populate column "m1$major_allele" > for ( i in 1:length(m1$major_allele)) { > if ( m1$Freq1[i] == m1$MAF[i]){ > m1$major_allele[i] = m1$Al1[i] > } > else{ > m1$major_allele[i] = m1$Al2[i] > } > } > > > Jim > > [[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] how to use by() ?
Hello, All! How might one accomplish this using the by() function? m1 is a data frame. # populate column "m1$major_allele" for ( i in 1:length(m1$major_allele)) { if ( m1$Freq1[i] == m1$MAF[i]){ m1$major_allele[i] = m1$Al1[i] } else{ m1$major_allele[i] = m1$Al2[i] } } Jim [[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.