Hello I have 2 data frames df1 and df2. I would like to create a new data frame new_df which will contain only the common rows based on the first 2 columns (chrN and start). The column score in the new data frame should be replaced with a column containing the average score (average_score) from df1 and df2. df1= data.frame(chrN= c(“chr1”, “chr1”, “chr1”, “chr1”, “chr2”, “chr2”, “chr2”), start= c(23, 82, 95, 108, 95, 108, 121), end= c(33, 92, 105, 118, 105, 118, 131), score= c(3, 6, 2, 4, 9, 2, 7)) df2= data.frame(chrN= c(“chr1”, “chr2”, “chr2”, “chr2” , “chr2”), start= c(23, 50, 95, 20, 121), end= c(33, 60, 105, 30, 131), score= c(9, 3, 7, 7, 3)) new_df= data.frame(chrN= c(“chr1”, “chr2”, “chr2”), start= c(23, 95, 121), end= c(33, 105, 131), average_score= c(6, 8, 5)) Thank you for your help Joseph ____________________________________________________________________________________ Never miss a thing. Make Yahoo your home page. [[alternative HTML version deleted]]
On 10/02/2008, joseph <jdsandjd at yahoo.com> wrote:> Hello > I have 2 data frames df1 and df2. I would like to create a > new data frame new_df which will contain only the common rows based on the first 2 > columns (chrN and start). The column score in the new data frame > should > be replaced with a column containing the average score (average_score) from df1 > and df2.Try this: (avoiding underscores) new.df <- merge(df1, df2, by=c('chrN','start')) new.df$average.score <- apply(df3[,c('score.x','score.y')], 1, mean, na.rm=T) As always, interested to see whether it can be done in one line... -- Dr. Mark Wardle Specialist registrar, Neurology Cardiff, UK
joseph <jdsandjd at yahoo.com> wrote in news:109232.80965.qm at web36905.mail.mud.yahoo.com:> I have 2 data frames df1 and df2. I would like to create a > new data frame new_df which will contain only the common rows based > on the first 2 columns (chrN and start). The column score in the new > data frame should > be replaced with a column containing the average score > (average_score) from df1 and df2. >> df1= data.frame(chrN= c("chr1", "chr1", "chr1", "chr1", "chr2", > "chr2", "chr2"), > start= c(23, 82, 95, 108, 95, 108, 121), > end= c(33, 92, 105, 118, 105, 118, 131), > score= c(3, 6, 2, 4, 9, 2, 7)) > > df2= data.frame(chrN= c("chr1", "chr2", "chr2", "chr2" , "chr2"), > start= c(23, 50, 95, 20, 121), > end= c(33, 60, 105, 30, 131), > score= c(9, 3, 7, 7, 3))Clunky to be sure, but this should worked for me: df3 <- merge(df1,df2,by=c("chrN","start") #non-match variables get auto-relabeled df3$avg.scr <- with(df3, (score.x+score.y)/2) # or mean( ) df3 <- df3[,c("chrN","start","avg.scr")] #drops the variables not of interest df3 chrN start avg.scr 1 chr1 23 6 2 chr2 121 5 3 chr2 95 8 -- David Winsemius
Hi I have a data frame df1 in which I would like to multiply col1 by 2. The way I did it does not allow me to keep the old data frame. How can I do this and be able to create a new data frame df2?> df1= data.frame(col1= c(3, 5, NA, 1), col2= c(4, NA,6,2))> df1col1 col2 1 3 4 2 5 NA 3 NA 6 4 1 2> df1$col1=df1$col1*2> df1col1 col2 1 6 4 2 10 NA 3 NA 6 4 2 2 ____________________________________________________________________________________ Be a better friend, newshound, and [[alternative HTML version deleted]]
df2 = df ? G. On Thu, Feb 14, 2008 at 09:12:23AM -0800, joseph wrote:> > > Hi > > I have a data frame df1 in which I would like to multiply col1 > by 2. > > > The way I did it does not allow me to keep the old data > frame. > > > How can I do this and be able to create a new data frame > df2? > > > > df1= data.frame(col1= c(3, 5, NA, 1), col2= c(4, NA,6, > 2)) > > > > df1 > > > col1 col2 > > > 1 3 4 > > > 2 5 NA > > > 3 NA 6 > > > 4 1 2 > > > > df1$col1=df1$col1*2 > > > > df1 > > > col1 col2 > > > 1 6 4 > > > 2 10 NA > > > 3 NA 6 > > > 4 2 2 > > > > > > ____________________________________________________________________________________ > Be a better friend, newshound, and > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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.-- Csardi Gabor <csardi at rmki.kfki.hu> UNIL DGM
If I understand: df2 <- transform(df1, col3=col1*2) On 14/02/2008, joseph <jdsandjd at yahoo.com> wrote:> > > Hi > > I have a data frame df1 in which I would like to multiply col1 > by 2. > > > The way I did it does not allow me to keep the old data > frame. > > > How can I do this and be able to create a new data frame > df2? > > > > df1= data.frame(col1= c(3, 5, NA, 1), col2= c(4, NA,6, > 2)) > > > > df1 > > > col1 col2 > > > 1 3 4 > > > 2 5 NA > > > 3 NA 6 > > > 4 1 2 > > > > df1$col1=df1$col1*2 > > > > df1 > > > col1 col2 > > > 1 6 4 > > > 2 10 NA > > > 3 NA 6 > > > 4 2 2 > > > > > > ____________________________________________________________________________________ > Be a better friend, newshound, and > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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. >-- Henrique Dallazuanna Curitiba-Paran?-Brasil 25? 25' 40" S 49? 16' 22" O
On Thursday 14 February 2008 06:12:23 pm joseph wrote: jo> I have a data frame df1 in which I would like to multiply col1 jo> by 2. jo> The way I did it does not allow me to keep the old data jo> frame. jo> jo> jo> How can I do this and be able to create a new data frame jo> df2? jo> jo> jo> > df1$col1=df1$col1*2 df$col3<-df1$col1*2 Stefan
On Thursday 14 February 2008 06:27:07 pm Stefan Grosse wrote: SG> df$col3<-df1$col1*2 ups it should be df1$col3<-df1$col1*2
Create the new data.frame and do the muliplying on it? df2 <- df1 df2[,1] <- df2[,1]*2 --- joseph <jdsandjd at yahoo.com> wrote:> > > Hi > > I have a data frame df1 in which I would like to > multiply col1 > by 2. > > > The way I did it does not allow me to keep the old > data > frame. > > > How can I do this and be able to create a new data > frame > df2? > > > > df1= data.frame(col1= c(3, 5, NA, 1), col2= c(4, > NA,6, > 2)) > > > > df1 > > > col1 col2 > > > 1 3 4 > > > 2 5 NA > > > 3 NA 6 > > > 4 1 2 > > > > df1$col1=df1$col1*2 > > > > df1 > > > col1 col2 > > > 1 6 4 > > > 2 10 NA > > > 3 NA 6 > > > 4 2 2 > > > > > > >____________________________________________________________________________________> Be a better friend, newshound, and > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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. >
Thanks. I have another question: In the following data frame df, I want to replace all values in col1 that are higher than 3 with NA. df= data.frame(col1=c(1:5, NA),col2= c(2,NA,4:7)) ----- Original Message ---- From: John Kane <jrkrideau@yahoo.ca> To: joseph <jdsandjd@yahoo.com>; r-help@r-project.org Cc: r-help@r-project.org Sent: Thursday, February 14, 2008 3:09:40 PM Subject: Re: [R] data frame question Create the new data.frame and do the muliplying on it? df2 <- df1 df2[,1] <- df2[,1]*2 --- joseph <jdsandjd@yahoo.com> wrote:> > >Hi> >I have a data frame df1 in which I would like to>multiply col1>by 2.> > >The way I did it does not allow me to keep the old>data>frame.> > >How can I do this and be able to create a new data>frame>df2?> > > >df1= data.frame(col1= c(3, 5, NA, 1), col2= c(4,>NA,6,>2))> > > >df1> > >col1 col2> > >1 3 4> > >2 5 NA> > >3 NA 6> > >4 1 2> > > >df1$col1=df1$col1*2> > > >df1> > >col1 col2> > >1 6 4> > >2 10 NA> > >3 NA 6> > >4 2 2> > > > > >>____________________________________________________________________________________>Be a better friend, newshound, and> > >[[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.>Connect with friends from any web browser - no download required. Try the new Canada Messenger for the Web BETA at ____________________________________________________________________________________ Looking for last minute shopping deals? [[alternative HTML version deleted]]
Hi, joseph wrote (15.2.2008):> Thanks. I have another question: > In the following data frame df, I want to replace all values in col1 > that are higher than 3 with NA. df= data.frame(col1=c(1:5, NA),col2> c(2,NA,4:7))My suggestion: x<-df$col1; x[ x>3 ]<-NA; df$col1<-x; rm(x) -Kimmo
... or in one step df <- transform(df, col1 = ifelse(col1 > 3, NA, col1)) -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of K. Elo Sent: Friday, 15 February 2008 4:29 PM To: r-help at r-project.org Subject: Re: [R] data frame question Hi, joseph wrote (15.2.2008):> Thanks. I have another question: > In the following data frame df, I want to replace all values in col1 > that are higher than 3 with NA. df= data.frame(col1=c(1:5, NA),col2> c(2,NA,4:7))My suggestion: x<-df$col1; x[ x>3 ]<-NA; df$col1<-x; rm(x) -Kimmo ______________________________________________ R-help at 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.