Dear all, I want to import just part of an excel data file into R. I would like to have the data imported without rownames or colume names. I used read.delim("clipboard", header=F). Somehow even though I added the argument "header=F", I still have the row names V1, V2, ..., Does anyone know how to fix this? Thanks very much in advance. Hannah [[alternative HTML version deleted]]
Hannah: 1. First of all, they are column names, not row names. 2. Second, no, you cannot fix it. All columns in a data frame *must* have names, and if none are obtained from the "import," the defaults you see will be provided. See ?data.frame for details of how columns are named. Columns of data of the same mode/class could be converted to a matrix, which does not need to have its columns named. But it's hard to see why one would want to do this. -- Bert On Wed, Aug 15, 2012 at 2:04 PM, li li <hannah.hlx at gmail.com> wrote:> Dear all, > I want to import just part of an excel data file into R. > I would like to have the data imported without > rownames or colume names. > I used read.delim("clipboard", header=F). Somehow even though > I added the argument "header=F", I still have the row names V1, V2, ..., > Does anyone know how to fix this? > Thanks very much in advance. > Hannah > > [[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.-- Bert Gunter Genentech Nonclinical Biostatistics Internal Contact Info: Phone: 467-7374 Website: http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm
HI, I guess you were talking about column names instead of rownames. If you have a data like the one below to read: dat1<-read.table(text=" ???????? beta0????? beta1? pvalor??????? Crom rs17????? 158.5980 12.252462 9.083193e-135??????? 1 rs46????? 163.3730? 3.304276? 3.279925e-06????????? 1 rs63????? 162.7924? 2.084678? 5.023893e-06????????? 1 rs24????? 162.4252? 1.837208? 5.509042e-06????????? 1 ",sep="",header=TRUE,row.names=NULL) Here, you have column names for four columns, so if you don't add row.names=NULL, then the first column will become the row names. Now, for the second case: column names will be automatically generated with data frames if your data doesn't have one. dat2<-read.table(text=" ???? 158.5980 12.252462 9.083193e-135??????? 1 ????? 163.3730? 3.304276? 3.279925e-06????????? 1 ???? 162.7924? 2.084678? 5.023893e-06????????? 1 ???? 162.4252? 1.837208? 5.509042e-06????????? 1 ",sep="",header=FALSE) colnames(dat2)<-NULL # will create NA's as column names. colnames(dat2)<-c(1,2,3,4) # will replace V1,V2, etc. with 1,2,.. Bu, converting to matrix, can solve this problem in this case, dat3<-as.matrix(dat2) ?colnames(dat3)<-NULL ?dat3 #???????? [,1]????? [,2]????????? [,3] [,4] #[1,] 158.5980 12.252462 9.083193e-135??? 1 #[2,] 163.3730? 3.304276? 3.279925e-06??? 1 #[3,] 162.7924? 2.084678? 5.023893e-06??? 1 #[4,] 162.4252? 1.837208? 5.509042e-06??? 1 Is there any specific reason to eliminate the column names?? A.K. ----- Original Message ----- From: li li <hannah.hlx at gmail.com> To: r-help <r-help at r-project.org> Cc: Sent: Wednesday, August 15, 2012 5:04 PM Subject: [R] Import Data from Excel Dear all, ? I want to import just part of an excel data file into R. ? I would like to have the data imported without rownames or colume names. ? I used read.delim("clipboard", header=F).? Somehow even though I added the argument "header=F", I still have the row names V1, V2, ..., Does anyone know how to fix this? ? Thanks very much in advance. ? ? ? Hannah ??? [[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.
On Aug 15, 2012, at 2:04 PM, li li wrote:> Dear all, > I want to import just part of an excel data file into R. > I would like to have the data imported without > rownames or colume names. > I used read.delim("clipboard", header=F). Somehow even though > I added the argument "header=F", I still have the row names V1, > V2, ..., > Does anyone know how to fix this? > Thanks very much in advance.You could use readLines() or you could use scan(). Perhaps: input <- scan(file="clipboard", what="numeric") If you know the number of columns, say 10, it could be: inpmat <- matrix( scan(file="clipboard", what="numeric"), ncol=10) -- David Winsemius, MD Alameda, CA, USA
On Aug 15, 2012, at 4:07 PM, David Winsemius wrote:> > On Aug 15, 2012, at 2:04 PM, li li wrote: > >> Dear all, >> I want to import just part of an excel data file into R. >> I would like to have the data imported without >> rownames or colume names. >> I used read.delim("clipboard", header=F). Somehow even though >> I added the argument "header=F", I still have the row names V1, >> V2, ..., >> Does anyone know how to fix this? >> Thanks very much in advance. > > You could use readLines() or you could use scan(). > > Perhaps: > > input <- scan(file="clipboard", what="numeric") > > If you know the number of columns, say 10, it could be: > > inpmat <- matrix( scan(file="clipboard", what="numeric"), ncol=10)Or perhaps: inpmat <- matrix( scan(file="clipboard", what=double() , ncol=10) Using the first version actually gives you character values, since "numeric" is of of type character. Different convention than most other reading functions. -- David Winsemius, MD Alameda, CA, USA