[R] How to merge string to DF

Stephen Tucker brown_emu at yahoo.com
Fri Aug 24 11:59:22 CEST 2007


This seems to work:

tmp <- aggregate(DF$y, list(DF$x, DF$f), mean)

tmp2 <- aggregate(DF$conc, list(DF$x, DF$f), paste,collapse=", ")
names(tmp2)[3] <- "var1"

final <- merge(tmp,tmp2)


--- Lauri Nikkinen <lauri.nikkinen at iki.fi> wrote:

> #Hi R-users,
> #I have an example DF like this:
> 
> y1 <- rnorm(10) + 6.8
> y2 <- rnorm(10) + (1:10*1.7 + 1)
> y3 <- rnorm(10) + (1:10*6.7 + 3.7)
> y <- c(y1,y2,y3)
> x <- rep(1:3,10)
> f <- gl(2,15, labels=paste("lev", 1:2, sep=""))
> g <- seq(as.Date("2000/1/1"), by="day", length=30)
> DF <- data.frame(x=x,y=y, f=f, g=g)
> DF$wdays <- weekdays(DF$g)
> DF$conc <- paste(DF$g, DF$wdays)
> DF
> 
> #Now I calculate group means
> 
> tmp <- aggregate(DF$y, list(DF$x, DF$f), mean)
> tmp
> 
> #After doing this, I want to merge string from DF$conc to tmp using DF$x
> and
> DF$y as an identifier
> #The following DF should look like this:
> 
>   Group.1 Group.2         x var1
> 1       1    lev1  6.607869 2000-01-01 Saturday, 2000-01-04 Tuesday,
> 2000-01-07 Friday, 2000-01-10 Monday etc.
> 2       2    lev1  6.598861 etc.
> 3       3    lev1  7.469262
> 4       1    lev2 27.488734
> 5       2    lev2 33.164037
> 6       3    lev2 34.466359
> 
> #How do I do this?
> 
> #Cheers,
> #Lauri
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at stat.math.ethz.ch 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.
>



More information about the R-help mailing list