[R] new comer's question

Chuck Berry cberry at tajo.ucsd.edu
Tue Jan 17 21:29:08 CET 2006


Chang Shen <Chang_Shen <at> progressive.com> writes:

> 
> 
> I am new to R.  I try to search the web but could not find the answer so I
> post it here asking for help.

Broad web searches may not be as helpful as following the advice in the posting
guide:

   http://www.R-project.org/posting-guide.html

Following the advice to "Do help.search("keyword") with different keywords (type
this at the R prompt)" would have led you to the help page for 'levels'. More on
this below.

> 
> I have a csv file looks like this: (between two ==== lines)
> ===========================
> 
> Machine Name,"Resource, Type","Resource, Sub-type","Resource,

The line wrapping here and below makes it harder for folks to help you!!

> Instance",Date,,Data ->,,,,,,
> ,0.041666667,,,,,,,,,,,
> 
> Time (HH:MM) ->,,,,,,0:00,0:15,0:30,0:45,1:00,1:15,1:30
> SCINFR06,Cache,Copy Read Hits %,,10-Jan-06,Cache->Copy Read Hits
> %,0.99,1,1,1,1,1,0.99
> SCINFR06,Cache,Data Map Hits %,,10-Jan-06,Cache->Data Map Hits
> %,1,1,1,1,1,1,1
> 
> Time (HH:MM) ->,,,,,,0:00,0:15,0:30,0:45,1:00,1:15,1:30
> SCINFR06,LogicalDisk,% Disk Read Time,C:,10-Jan-06,LogicalDisk->% Disk Read
> Time->C:,2.14,1.52,1.94,1.68,2.52,2.05,2.66
> SCINFR06,LogicalDisk,% Disk Read Time,D:,10-Jan-06,LogicalDisk->% Disk Read
> Time->D:,0.04,0,0,0.08,0,0,0
> SCINFR06,LogicalDisk,% Disk Read
> Time,HarddiskVolume1,10-Jan-06,LogicalDisk->% Disk Read
> Time->HarddiskVolume1,0,0,0,0,0,0,0
> SCINFR06,LogicalDisk,% Disk Read Time,_Total,10-Jan-06,LogicalDisk->% Disk
> Read Time->_Total,0.72,0.51,0.65,0.59,0.84,0.68,0.89
> 
> ==============================
> 
> First I load it by read.table call:
> 
> myArray <- read.table("c:/mydata.csv",sep=",");
> 

Was the first row intended as a 'header'. If so, 

 myArray <- read.table("c:/mydata.csv", sep="," , header=TRUE )

might be a better start.

FYI, 'myArray' is not an array:

R > is.array(myArray)
[1] FALSE

If this surprises you, you need to review the read.table help page and the "R
Data Import/Export Manual" Chapter 2.

> After this,  the array element myArray[1,2] looks like this
> 
> >myArray[1,2]
> [1] Resource, Type
> Levels:  0.041666667 Cache LogicalDisk Resource, Type
> 
> Here are the questions:
> (1) What does Levels mean?

It looks like the 'levels' attribute printed in a formaated style:

R > levels( myArray[,2] )
[1] ""               "0.041666667"    "Cache"          "LogicalDisk"   
[5] "Resource, Type"
 



> (2) When I try to split the string "Resource, Type", which is myArray[1,2],

myArray[1,2] is NOT a 'string'.

R > is.character( myArray[1,2] )
[1] FALSE

but it is a 'factor':

R > class( myArray[1,2] )
[1] "factor"


> using function strsplit(), I got error:
> 
> > w<-strsplit(myArray[1,2],",")
> Error in strsplit(x, as.character(split), as.logical(extended),
> as.logical(fixed),  :
>         non-character argument in strsplit()
> 
> Then I tried this:
> 
> > y<-myArray[1,2]
> > y
> [1] Resource, Type
> Levels:  0.041666667 Cache LogicalDisk Resource, Type
> > w<-strsplit(y,",")
> Error in strsplit(x, as.character(split), as.logical(extended),
> as.logical(fixed),  :
>         non-character argument in strsplit()
> 
> But the following call does not cause any error.
> 
> >  y<-"Resource, Type"
> > w<-strsplit(y,",")
> > w
> [[1]]
> [1] "Resource" " Type"
> 
> what is wrong?

You need to convert the 'factor' object to a 'character' object:

R > strsplit( as.character( myArray[1,2] ), "," )
[[1]]
[1] "Resource" " Type"   


> 
> Thanks
> 
> Chang
> 
> ______________________________________________
> R-help <at> stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
> 
>




More information about the R-help mailing list