[R] Losing custom attributes

David Winsemius dwinsemius at comcast.net
Wed Nov 23 16:46:53 CET 2011


On Nov 23, 2011, at 10:19 AM, Smart Guy wrote:

> Hi Eric,
>       Thanks for the reply.
> Actually, I am looking for a way so that my custom attributes are  
> not lost
> after the row insert operation. It can be rbind() or some other way.

dfm <- dfm[c(1,1:nrow(dfm), ]
dfm[ 1, ] <- c(age=16, weight= 42)


>
> Regards,
> SG
>
> On 23 November 2011 18:07, Eric Lecoutre <ericlecoutre at gmail.com>  
> wrote:
>
>>
>> I guess rbind takes attributes from the first dataframe.
>> I tried to rbind with mydata[NULL,,drop=FALSE] but rbind help  
>> states that
>> empy data frames are dropped from rbind.
>> So that as a workaround, I have:
>>
>>
>> mydata*<-* rbind
>> (mydata[1,,drop=FALSE],newrow, mydata)[-1,]
>>
>> Eric
>>
>>
>> On 23 November 2011 13:20, Smart Guy <smartguy3k at gmail.com> wrote:
>>
>>> Hi All,
>>>          I was adding a new row of data to my data frame using  
>>> rbind(). I
>>> was surprised to see that after adding new row, I lost my data  
>>> frame level
>>> attibute as well as  col level attribute. Please help me to insert  
>>> a new
>>> row at frist or middle position so that my custom attribute is not  
>>> lost.
>>>
>>> Here is what I did.
>>>
>>> age<-c(15,20,18)
>>> weight<-c(40,42,30)
>>>
>>> ### creating my data frame ####
>>> mydata <- data.frame(age,weight)
>>>
>>> ### creating data frame level attribute ####
>>> attr(mydata,"myattr")<-c("myinfo")
>>>
>>> ### creating col level attribute for 'age' column  ###
>>> attr(mydata$age,"mycolattr")<-c("mycolinfo")
>>>
>>> #### Checking attributes  ###
>>> attributes(mydata)
>>> attributes(mydata$age)
>>>
>>> ### creating new row  #####
>>> newrow <- data.frame(age=16, weight= 42)
>>>
>>> #### Inserting newrow as first row to my data frame ####
>>> mydata<- rbind(newrow, mydata)
>>>
>>> #### Checking attributes again ###  I lost my custom attributes
>>> attributes(mydata$age)
>>> attributes(mydata)
>>>
>>>
>>> Thanks in advance,
>>>
>>>
>>> --
>>> SG
>>>
>>>       [[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<http://www.r-project.org/posting-guide.html 
>>> >
>>> and provide commented, minimal, self-contained, reproducible code.
>>>
>>
>>
>>
>> --
>> Eric Lecoutre
>> Consultant - Business & Decision
>> Business Intelligence & Customer Intelligence
>>
>
>
>
> -- 
> SG
>
> 	[[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.

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list