[R] Fwd: R basic data manipulation Queries

arun smartpink111 at yahoo.com
Wed Mar 19 16:24:08 CET 2014


HI,
It seems like you used ?attach().  Better would be to use ?with.
set.seed(45)
dat1 <- as.data.frame(matrix(sample(LETTERS,80,replace=TRUE),20,4))
lapply(dat1,levels) ##get the levels of all the dataset variables in a list

 with(dat1,levels(V1))


A.K.





I have another question, at the moment I am looking at the levels for some of my variables in my dataset. 
Instead of typing "levels" statement everytime, is there a shortcut for doing that? 

So at the moment I am writing 
levels(time) 
levels(hour) 
level(date) 
levels(qrtr) 
and so on... 
can I do all this in one step? 





> -----Original Message-----
> From: pavneet17 at yahoo.co.uk
> Sent: Tue, 18 Mar 2014 10:10:38 +0000
> To: r-help at r-project.org
> Subject: [R] Fwd: R basic data manipulation Queries
> 
> 
> 
> 
> 
> 
> Sent from Samsung Mobile
> 
> -------- Original message --------
> Subject: R basic data manipulation Queries
> From: Pavneet Arora <pavneet.arora at uk.rsagroup.com>
> To: pavneet17 at yahoo.co.uk
> CC:
> 
> Hello Guys
> 
> I am new in R, so please excuse the really basic questions. I have tried
> reading numeral tutorials, but I am still stuck.
> Question 1:
> If I perform correlation on my data [cor(nums2)] or try to produce
> variance matrix [var(nums2)]. The output comes in R console. Is there any
> way I can make it go directly to excel somehow?

Not exactly but fairly easily. Have a look at the R-site and go to the Manuals section.  There is a R Import and Export manual there.
> 
> Question 2:
> Also is there any way I can permanently change the variable name in a
> data frame.
> I basically imported my dataset from SAS using "read.ssd" function in
> library(foreign). However, this package cuts off the variable names and
> only allows 8 characters! So that means I will have to rename some of my
> variable names, so they make more sense as to what it is.
> 
> part (a):
> At the moment, I did the following to change the variable names, using
> library(plyr). First of all, is this the most succint way of doing this?
> new_acc <- rename(acc_mod,c("NAME_OF_"="weekName", "ACCIDENT"="AccSev",
> "YEARMONH"="YrMonHr",
> "X_1ST_ROA"="1RdCls.N", "ROAD_TYP"="RdType.N", "LOCATION"="LocEast"))
> 
> part (b):
> Secondly, I wanted to do the above, to change the name permanently - but
> I don't think it worked, because when I do the following, I get:
> class(LocEast) # New name of "LOCATION"
> class(LOCATION)
> R Output:
>> class(LocEast)
> Error: object 'LocEast' not found
>> class(LOCATION)
> [1] "numeric"
> 
> Thanks so much!B
>     [[alternative HTML version deleted]]
> 

I have never used plyr for thhis but just
names(aac.mod)  <-  c("Newname1, "Newname2") and so on for a new name for each column in the data.frame should do it.

____________________________________________________________
Protect your computer files with professional cloud backup.
Get PCRx Backup and upload unlimited files automatically. 
Learn more at http://backup.pcrx.com/mail


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





More information about the R-help mailing list