Re: [R] Combining series of variables using identifier

2008-02-26 Thread Henrique Dallazuanna
Try this:

foo <- function(data, ...)
{
patt <- grep("var[0-9]$", names(data), value=T)
res <- paste(sapply(data[, patt], as.character), sapply(data[,
paste(patt, "lab", sep="_")], as.character), sep=": ")
cbind(a, matrix(res, ncol=length(patt), dimnames=list(NULL,
paste(patt, "new", sep="_"
}

foo(a)

On 26/02/2008, Lauri Nikkinen <[EMAIL PROTECTED]> wrote:
> R users,
>
>  I have df like this
>
>  a <- data.frame(indx =1:20,
> var1 =rep(c("I20", "I40", "A50", "B60"), each=5),
> var1_lab= rep(c("cat", "dog", "mouse", "horse"), each=5),
> var2 =rep(c("B20", "X40", "D50", "G60"), each=5),
> var2_lab= rep(c("car", "bicycle", "train", "bus"), 
> each=5))
>  str(a)
>
>  I'd like to create new variables by combining "varX" and "varX_lab" like 
> this:
>
>  a$var1_new <- factor(paste(a$var1, a$var1_lab, sep=": "))
>  a$var2_new <- factor(paste(a$var2, a$var2_lab, sep=": "))
>  a
>
>  But, in the real world, I have multiple df:s and many of variables
>  named by the same manner. Is there a possibility to create a function
>  which combines these variables and creates new ones into the same df
>  using "varX" as an identifier?
>
>  Many thanks,
>  Lauri
>
>  __
>  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.
>


-- 
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40" S 49° 16' 22" 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] Combining series of variables using identifier

2008-02-26 Thread K. Elo
Hi,

if the columns always follow the same order (indx,var#, var#_lab ...), 
then You could use the column numbers and do the following:

1) CN<-colnames(df)[#] (#=colnum of 'var#')
2) df$NEW<-(here the expression to create the new variable)
3) colnames(df)[ncol(df)]<-c(paste(CN,"new",sep="_"))

This can easily be built in a loop.

Hope this helps,
Kimmo

Lauri Nikkinen kirjoitti viestissään (26.02.2008):
> R users,
>
> I have df like this
>
> a <- data.frame(indx =1:20,
> var1 =rep(c("I20", "I40", "A50", "B60"),
> each=5), var1_lab= rep(c("cat", "dog", "mouse", "horse"), each=5),
> var2 =rep(c("B20", "X40", "D50", "G60"), each=5), var2_lab=
> rep(c("car", "bicycle", "train", "bus"), each=5)) str(a)
>
> I'd like to create new variables by combining "varX" and "varX_lab"
> like this:
>
> a$var1_new <- factor(paste(a$var1, a$var1_lab, sep=": "))
> a$var2_new <- factor(paste(a$var2, a$var2_lab, sep=": "))
> a
>
> But, in the real world, I have multiple df:s and many of variables
> named by the same manner. Is there a possibility to create a function
> which combines these variables and creates new ones into the same df
> using "varX" as an identifier?
>
> Many thanks,
> Lauri
>
> __
> 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] Combining series of variables using identifier

2008-02-26 Thread Lauri Nikkinen
R users,

I have df like this

a <- data.frame(indx =1:20,
var1 =rep(c("I20", "I40", "A50", "B60"), each=5),
var1_lab= rep(c("cat", "dog", "mouse", "horse"), each=5),
var2 =rep(c("B20", "X40", "D50", "G60"), each=5),
var2_lab= rep(c("car", "bicycle", "train", "bus"), each=5))
str(a)

I'd like to create new variables by combining "varX" and "varX_lab" like this:

a$var1_new <- factor(paste(a$var1, a$var1_lab, sep=": "))
a$var2_new <- factor(paste(a$var2, a$var2_lab, sep=": "))
a

But, in the real world, I have multiple df:s and many of variables
named by the same manner. Is there a possibility to create a function
which combines these variables and creates new ones into the same df
using "varX" as an identifier?

Many thanks,
Lauri

__
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.