Dear helpers, how can I extract only the values from a row in a data frame? Using [X,] doesn't do the trick:> data.frame(letters[1:10],letters[11:20])->my.data > my.data[1,]letters.1.10. letters.11.20. 1 a k I would like to be able to extract only the values "a" and "k" without getting the row names and column names with them. I'm asking because I want to assign the character values from a row as the names of the columns of the data frame. Best Sverre
Hi Sverre, On Mon, Jul 11, 2011 at 12:48 PM, Sverre Stausland <johnsen at fas.harvard.edu> wrote:> Dear helpers, > > how can I extract only the values from a row in a data frame? Using > [X,] doesn't do the trick: > >> data.frame(letters[1:10],letters[11:20])->my.data >> my.data[1,] > ?letters.1.10. letters.11.20. > 1 ? ? ? ? ? ? a ? ? ? ? ? ? ?k > > I would like to be able to extract only the values "a" and "k" without > getting the row names and column names with them. I'm asking because I > want to assign the character values from a row as the names of the > columns of the data frame.You're confusing display with value. R extracts the first row only, but when it is *printed to the screen* it has all the other parts shown to help the viewer. They aren't really part of the value extracted. But I bet you tried it before asking, and it didn't work, so you assumed it was the display values that messed up your attempt. I think you've actually run into R's default behavior: character columns are stored as factors unless you specify otherwise. So try this:> my.data <- data.frame(letters[1:10],letters[11:20], stringsAsFactors=FALSE) > my.other.data <- data.frame(1:5, 1:5) > colnames(my.other.data) <- my.data[1,] > my.other.dataa k 1 1 1 2 2 2 3 3 3 4 4 4 5 5 5 Sarah -- Sarah Goslee http://www.functionaldiversity.org
A) Please remove "->" from your vocabulary. Also, always put spaces on either side of an assignment so the parser doesn't get confused. B) This is a pretty basic question. ("a"==my.data$letters.1.10.) is a vector of logical values. ( ("a"==my.data$letters.1.10.) & ("k"==my.data$letters.1.20.) ) is a (potentially) more restrictive vector of logical values. If you index just one vector you can get one value: my.data$letters.1.10.[ ("a"==my.data$letters.1.10.) & ("k"==my.data$letters.1.20.) ], though it seems easier to just specify "a". How about rownames(my.data) <- my.data$letters.1.10. ? --------------------------------------------------------------------------- Jeff Newmiller The ..... ..... Go Live... DCN:<jdnewmil@dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/Batteries O.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k --------------------------------------------------------------------------- Sent from my phone. Please excuse my brevity. Sverre Stausland <johnsen@fas.harvard.edu> wrote: Dear helpers, how can I extract only the values from a row in a data frame? Using [X,] doesn't do the trick:> data.frame(letters[1:10],letters[11:20])->my.data > my.data[1,]letters.1.10. letters.11.20. 1 a k I would like to be able to extract only the values "a" and "k" without getting the row names and column names with them. I'm asking because I want to assign the character values from a row as the names of the columns of the data frame. Best Sverre _____________________________________________ R-help@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. [[alternative HTML version deleted]]