Hello Gurus,
I am still new to R. Here is my issue.
I was trying to add column to data frame that was populated by read.spss().
When I used cbind to add a new variable(column).
library(foreign)
mydf<-read.spss(file="C:/myspss.sav",use.value.labels=FALSE,
to.data.frame=TRUE,use.missings=FALSE)
attr(mydf,"variable.labels")
## it gives you all the labels
##Adding a new variable(col) to mydf
newcolm <- list(frt="grapes")
mydf <- cbind(mydf, newrow)
##Now checking to see labels again
attr(mydf,"variable.labels")
## And you get NULL
Can anyone please help me to achieve this without loosing any default
attribute that was there?
Thanks and Regards
--
SmartG
[[alternative HTML version deleted]]
On 10/29/2011 06:54 PM, Smart Guy wrote:> Hello Gurus, > I am still new to R. Here is my issue. > I was trying to add column to data frame that was populated by read.spss(). > When I used cbind to add a new variable(column). > > library(foreign) > mydf<-read.spss(file="C:/myspss.sav",use.value.labels=FALSE, > to.data.frame=TRUE,use.missings=FALSE) > attr(mydf,"variable.labels") > ## it gives you all the labels > > ##Adding a new variable(col) to mydf > newcolm<- list(frt="grapes") > mydf<- cbind(mydf, newrow) > > ##Now checking to see labels again > attr(mydf,"variable.labels") > > ## And you get NULL > > Can anyone please help me to achieve this without loosing any default > attribute that was there?Hi SmartG, I assume that the "newcolm"-"newrow" is just a typo in the example above. Assuming that you are managing to get your new column in "mydf", you can add the new variable name like this: oldvl<-attr(mydf,"variable.labels") mydf<-cbind(mydf,newrow) attr(mydf,"variable.labels")<-c(oldvl,"newvar") Jim
Possibly Parallel Threads
- Problem in writing a R data frame to Excel format using RODBC package
- Losing custom attributes
- SPSS F-test on change in R square between hierarchical models
- The use of period in function names and variable names
- Is there way to add a new row to a data frame in a specific location