Dear all,
I am trying to add a value to a dataframe and name the row with a number.? I
have tried row.name, rowname, and attr(x,"row.names") but none seem to
work.? It
seems like it should be simple, so not sure why I can't get it to work.? Any
suggestions?
Thanks,
Tim
x<-seq(1,20,2)
y<-seq(20,1,-2)
xy<-data.frame(x,y)
xy<-rbind(xy,c(0,0))
#Threeattempts that fail
row.names(xy[11,])<-c("12")
rownames(xy[11,])<-c("12")
attr(xy[11,], "row.names")<-c("12")
?Tim Clark
Marine Ecologist
National Park of American Samoa
Tim,
The row names have only one dimension, so for example
row.names(xy)[11]<-"New rname"
will work
best,
Jon
-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
On Behalf Of Tim Clark
Sent: 27. september 2010 04:41
To: r-help at r-project.org
Cc: tim_clark at nps.gov
Subject: [R] Adding row name to dataframe
Dear all,
I am trying to add a value to a dataframe and name the row with a number.? I
have tried row.name, rowname, and attr(x,"row.names") but none seem to
work.? It
seems like it should be simple, so not sure why I can't get it to work.? Any
suggestions?
Thanks,
Tim
x<-seq(1,20,2)
y<-seq(20,1,-2)
xy<-data.frame(x,y)
xy<-rbind(xy,c(0,0))
#Threeattempts that fail
row.names(xy[11,])<-c("12")
rownames(xy[11,])<-c("12")
attr(xy[11,], "row.names")<-c("12")
?Tim Clark
Marine Ecologist
National Park of American Samoa
______________________________________________
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.
Hello Tim,
Either of these variations on your example should work...
rownames(xy)[11] <- 12
rownames(xy)[11] <- "12"
It's just like assigning values to any character vector, so you can
also do things like...
rownames(xy) <- a.vector.of.all.the.row.names
rownames(xy)[1:10] <- paste("foo", 1:10, sep="")
Michael
On 27 September 2010 12:41, Tim Clark <mudiver1200 at yahoo.com>
wrote:> Dear all,
>
> I am trying to add a value to a dataframe and name the row with a number.?
I
> have tried row.name, rowname, and attr(x,"row.names") but none
seem to work.? It
> seems like it should be simple, so not sure why I can't get it to
work.? Any
> suggestions?
>
> Thanks,
>
> Tim
>
>
>
> x<-seq(1,20,2)
> y<-seq(20,1,-2)
> xy<-data.frame(x,y)
> xy<-rbind(xy,c(0,0))
>
> #Threeattempts that fail
> row.names(xy[11,])<-c("12")
> rownames(xy[11,])<-c("12")
> attr(xy[11,], "row.names")<-c("12")
>
>
> ?Tim Clark
>
> Marine Ecologist
> National Park of American Samoa
>
>
>
>
> ______________________________________________
> 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.
>