[R] Setting the names of a data.frame
Santosh Srinivas
santosh.srinivas at gmail.com
Tue Nov 2 16:09:33 CET 2010
Thanks.
Actually the sHeaders was a line in tData itself ...
I just did sHeaders = tData [1,]
How can I can build it without factors like your first suggestions?
-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On
Behalf Of Ivan Calandra
Sent: 02 November 2010 20:22
To: r-help at r-project.org
Subject: Re: [R] Setting the names of a data.frame
Hi,
The problem is that all your columns of sHeaders are factors. It might
be better to set stringsAsFactors to FALSE when you build it.
Or you can do it with a for loop like this:
for (i in 1:length(sHeaders)){
names(tData)[i] <- as.character(sHeaders[1,i])
}
Or with lapply:
names(tData) <- unlist(lapply(sHeaders[1, ], FUN=as.character))
HTH,
Ivan
Le 11/2/2010 14:58, Santosh Srinivas a écrit :
> I have tData as below. I need to set the names with the headers from the
> first row in sHeaders
> Sorry .. forgot how to set the names from row in another data frame .. pls
> advise.
>
> names(tData) = sHeaders[1,] does not work correctly
>
> Also, why doesn't drop.levels(sHeaders) not work?
>
> dput(tData)
> structure(list(V1 = structure(c(3L, 1L, 1L, 2L), .Label = c("P H Ravi
> Kumar",
> "Rahul Kumar Singh", "Ramu GSV"), class = "factor"), V2 = structure(c(1L,
> 3L, 3L, 2L), .Label = c("05/10/2010", "09/09/2010", "30/09/2010"
> ), class = "factor"), V3 = structure(c(2L, 1L, 1L, 2L), .Label = c("B",
> "S"), class = "factor"), V4 = structure(c(2L, 3L, 3L, 1L), .Label =
> c("2120",
> "4000", "11000"), class = "factor"), V5 = structure(c(1L, 2L,
> 2L, 1L), .Label = c("", "0.01"), class = "factor"), V6 = structure(c(2L,
> 3L, 3L, 1L), .Label = c("765", "1000", "11000"), class = "factor"),
> V7 = structure(c(1L, 2L, 2L, 1L), .Label = c("", "0.01"), class =
> "factor")), .Names = c("V1",
> "V2", "V3", "V4", "V5", "V6", "V7"), row.names = 5:8, class =
"data.frame")
>
>
> dput(sHeaders)
> structure(list(V1 = structure(1L, .Label = c("Name of Acquirer / Seller",
> "Qty", "Ramu GSV"), class = "factor"), V2 = structure(3L, .Label =
> c("05/10/2010",
> "%", "Transaction Date"), class = "factor"), V3 = structure(1L, .Label =
> c("Buy /Sale",
> "Qty", "S"), class = "factor"), V4 = structure(3L, .Label = c("4000",
> "%", "No.of Shares Transacted"), class = "factor"), V5 = structure(2L,
> .Label = c("",
> "Holding after Transaction"), class = "factor"), V6 =
structure(NA_integer_,
> .Label = "1000", class = "factor"),
> V7 = structure(NA_integer_, .Label = "", class = "factor")), .Names =
> c("V1",
> "V2", "V3", "V4", "V5", "V6", "V7"), row.names = 3L, class = "data.frame")
>
>
> Thanks very much.
>
> ______________________________________________
> 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.
>
--
Ivan CALANDRA
PhD Student
University of Hamburg
Biozentrum Grindel und Zoologisches Museum
Abt. Säugetiere
Martin-Luther-King-Platz 3
D-20146 Hamburg, GERMANY
+49(0)40 42838 6231
ivan.calandra at uni-hamburg.de
**********
http://www.for771.uni-bonn.de
http://webapp5.rrz.uni-hamburg.de/mammals/eng/mitarbeiter.php
______________________________________________
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