I read a table as follows:> F1 <- read.table("Rtext3.txt") > F1Price Floor Area Rooms Age Cent.heat a 52.00 111 830 5 6.2 no b 54.75 128 710 5 7.5 no c 57.50 101 1000 5 4.2 no d 57.50 131 690 6 8.8 no e 59.75 93 900 5 1.9 yes As it is seen, the rows have a name. However I don't know how to access a particular element from that name, for instance,> attach(F1) > Price[1] 52.00 54.75 57.50 57.50 59.75> Price[["a"]]Error en Price[["a"]] : sub?ndice fuera de los l?mites> Price$aError en Price$a : $ operator is invalid for atomic vectors> Price['a'][1] NA> Price["a"][1] NA If I cannot access particular elements of the frame components by the row names, is there any use of having the rows named? Thanks, Sergio.
R. Michael Weylandt <michael.weylandt@gmail.com>
2011-Nov-17 00:31 UTC
[R] Named rows in a table (data frame) read from a file
Don't use attach. I know it's tempting, but it's really not worth it in the long run. (Cf. Genesis 3, ff.) This works: F1["a", "Price"] Incidentally the reason yours didn't work is because the row names are part of F1, not Price so attach() doesn't carry them along. In short, row names good; attach bad. Michael On Nov 16, 2011, at 7:11 PM, JulioSergio <juliosergio at gmail.com> wrote:> I read a table as follows: > >> F1 <- read.table("Rtext3.txt") >> F1 > Price Floor Area Rooms Age Cent.heat > a 52.00 111 830 5 6.2 no > b 54.75 128 710 5 7.5 no > c 57.50 101 1000 5 4.2 no > d 57.50 131 690 6 8.8 no > e 59.75 93 900 5 1.9 yes > > As it is seen, the rows have a name. However I don't know how to access a > particular element from that name, for instance, > >> attach(F1) >> Price > [1] 52.00 54.75 57.50 57.50 59.75 >> Price[["a"]] > Error en Price[["a"]] : sub?ndice fuera de los l?mites >> Price$a > Error en Price$a : $ operator is invalid for atomic vectors >> Price['a'] > [1] NA >> Price["a"] > [1] NA > > If I cannot access particular elements of the frame components by the row names, > is there any use of having the rows named? > > Thanks, > > Sergio. > > ______________________________________________ > 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.
Sarah Goslee
2011-Nov-17 00:35 UTC
[R] Named rows in a table (data frame) read from a file
Hi, On Wed, Nov 16, 2011 at 7:11 PM, JulioSergio <juliosergio at gmail.com> wrote:> I read a table as follows: > >> F1 <- read.table("Rtext3.txt") >> F1 > ?Price Floor Area Rooms Age Cent.heat > a 52.00 ? 111 ?830 ? ? 5 6.2 ? ? ? ?no > b 54.75 ? 128 ?710 ? ? 5 7.5 ? ? ? ?no > c 57.50 ? 101 1000 ? ? 5 4.2 ? ? ? ?no > d 57.50 ? 131 ?690 ? ? 6 8.8 ? ? ? ?no > e 59.75 ? ?93 ?900 ? ? 5 1.9 ? ? ? yes > > As it is seen, the rows have a name. However I don't know how to access a > particular element from that name, for instance, > >> attach(F1)That's your biggest problem. Don't attach your dataframe.>> Price > [1] 52.00 54.75 57.50 57.50 59.75 >> Price[["a"]] > Error en Price[["a"]] : sub?ndice fuera de ?los l?mites >> Price$a > Error en Price$a : $ operator is invalid for atomic vectors >> Price['a'] > [1] NA >> Price["a"] > [1] NA > > If I cannot access particular elements of the frame components by the row names, > is there any use of having the rows named?Sure you can. It works just like any other indexing of data frames, with the caveat that a data frame is really a list with the columns as elements, so there are indexing options not available for rows. Since you didn't provide any data, here's a toy example:> test <- data.frame(X=1:3, Y=4:6, Z=7:9) > rownames(test) <- c("a", "b", "c") > testX Y Z a 1 4 7 b 2 5 8 c 3 6 9> test["a",]X Y Z a 1 4 7> test["a", "Y"][1] 4> test["a",][3]Z a 7 Sarah> Thanks, > > Sergio. >-- Sarah Goslee http://www.functionaldiversity.org