Hi all,
I'm a new R user and am confused about how R behaves when converting a vector
to a data frame when using the data.frame function. I'm specifically
interested in cases where the vector is expressed as a subset of another data
frame. For example, say I want to create a data frame from the last three rows
of the third column of the data frame, d, that I've created below:
a<-(1:10)
b<-(11:20)
c<-(21:30)
d<-data.frame(a,b,c)
To do that, I know that I could do:
e<-d[8:10,"c"]
f<-data.frame(e)
However, I would like for the single column in the data frame, f, to be named
"c". Obviously, I could just use the vector, c<-d[8:10,"c"], in place of the
vector e. However, I wonder why I can't do:
g<-data.frame(d[8:10,"c"])
This expression returns the proper values, but the resulting variable is named
"d.8.10...c.." and not "c" as I expected it to be named.
Could someone explain the mechanics of this statement and tell me why it
produced such an oddly named variable? I'm especially confused as to why I get
the result I expect if I use the data.frame function on multiple vectors, as
in:
g2<-data.frame(d[8:10,c("b","c")])
which produces a data frame with columns named "b" and "c".
Many thanks in advance,
Alec
[[alternative HTML version deleted]]
______________________________________________
[email protected] 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.