s_elternmz <- function(Var="balt")
{
  Dg_a<-mz[,c("asbhh","apkz",Var)]
  colnames(Dg_a)[colnames(Dg_a)=="apkz"]<-"bpkzm"
  colnames(Dg_a)[colnames(Dg_a)==Var]<-paste(Var,"_m",sep="")
  mz_int<-merge(mz,Dg_a,by=c("asbhh","bpkzm"),all.x=T)
  colnames(Dg_a)[colnames(Dg_a)=="bpkzm"]<-"bpkzv"

colnames(Dg_a)[colnames(Dg_a)==paste(Var,"_m",sep="")]<-paste(Var,"_v",sep="")
  mz_int<-merge(mz_int,Dg_a,by=c("asbhh","bpkzv"),all.x=T)
  mz_int <-
mz_int[order(mz_int$asbhh,mz_int$apkz),c(colnames(mz),paste(Var,"_m",sep=""),paste(Var,"_v",sep=""))]
 }

Thank you for your answers!

Jeff, thanks very much for the tip with the <<- instead of using <- to save
the objects outside of the function, as well! I find this very usefull and
convenient, also if I decide to choose more objects.

David, I didn't change Var to Var_v in one step because first i am merging
mz with the first Dg_a object (containing Var_m) and afterwards I am
merging the newly created mz_int with the Dg_a object which is now
containing Var_v. In this way I get both new colums (Var_m and Var_v).

But what I thougt you ment was that concerning these two lines ...

 colnames(Dg_a)[colnames(Dg_a)=="apkz"]<-"bpkzm"
 colnames(Dg_a)[colnames(Dg_a)==Var]<-paste(Var,"_m",sep="")

 ... there is a way to change "apkz" to "bpkzm" AND Var to "Var_m" in one
step. Do you know if and how this possible?

Marion



2012/2/14 David Winsemius <dwinsemius@comcast.net>

>
> On Feb 14, 2012, at 10:59 AM, Marion Wenty wrote:
>
>  Thank you very much for your help, David!
>>
>> Now I have got an object which I can work with.
>>
>> You mentioned that I took two steps to create the new column names.
>> I had tried doing it in one step but couldn't find out how. Could you
>> help me with that, as well?
>>
>> Thank you very much!
>>
>> Marion
>>
>>
>> 2012/2/14 David Winsemius <dwinsemius@comcast.net>
>>
>> On Feb 14, 2012, at 9:20 AM, Marion Wenty wrote:
>>
>> Dear R-helpers,
>>
>> I created an own function which looks like this
>>
>> s_elternmz <- function(Var="balt")
>>
>>   {
>>  Dg_a<-mz[,c("asbhh","apkz",**Var)]
>>  colnames(Dg_a)[colnames(Dg_a)=**="apkz"]<-"bpkzm"
>>  colnames(Dg_a)[colnames(Dg_a)=**=Var]<-paste(Var,"_m",sep="")
>>
>
> The above line seemed to be the first change to make <Var>_m
>
>
>   mz_int<-merge(mz,Dg_a,by=c("**asbhh","bpkzm"),all.x=T)
>>  colnames(Dg_a)[colnames(Dg_a)=**="bpkzm"]<-"bpkzv"
>>
>> colnames(Dg_a)[colnames(Dg_a)=**=paste(Var,"_m",sep="")]<-**
>> paste(Var,"_v",sep="")
>>
>
> Then this line changed <Var>_m to become <Var>_v. Why not change it to
> <Var>_v in the first place?
>
>
>
>> Why take two steps to change that column name?
>>
>>
>
>>
>>
> David Winsemius, MD
> West Hartford, CT
>
>

	[[alternative HTML version deleted]]

