Hi I want to extract a row from a data.frame but I want that object to be a vector . After trying some different ways I end up always with a data.frame or with the wrong vector. Any pointers? x <- data.frame(a = factor(c('a',2,'b')), b = c(4,5,6)) I want to get "a" "4" I tried: as.vector(x[1,]) a b 1 a 4 (resulting in a data.frame even after in my mind having coerced it into a vector!) as.vector(c[1,], numeric='character') [1] "2" "4" (almost what I want, except that "2" instead of "a" - I guess this as to do with levels and factors) Thanks for any help> R.Version()$platform [1] "powerpc-apple-darwin6.8" $arch [1] "powerpc" $os [1] "darwin6.8" $system [1] "powerpc, darwin6.8" $status [1] "" $major [1] "2" $minor [1] "0.1" $year [1] "2004" $month [1] "11" $day [1] "15" $language [1] "R"
A data frame is a list, and a list is a vector. Once you understand that, yoy may understand what you are seeing. as.matrix(x)[1,] seems to be one of the easiest ways to get what you want On Tue, 23 Nov 2004, Tiago R Magalhaes wrote:> Hi > > I want to extract a row from a data.frame but I want that object to be a > vector . After trying some different ways I end up always with a data.frame > or with the wrong vector. Any pointers? > > x <- data.frame(a = factor(c('a',2,'b')), b = c(4,5,6)) > I want to get > "a" "4" > > I tried: > > as.vector(x[1,]) > a b > 1 a 4 > (resulting in a data.frame even after in my mind having coerced it into a > vector!) > > as.vector(c[1,], numeric='character')Eh? I get an error there.> [1] "2" "4" > (almost what I want, except that "2" instead of "a" - I guess this as to do > with levels and factors)-- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
Is this what you want?> as.matrix(x[1,])[1,]a b "a" "4" HTH, Andy> From: Tiago R Magalhaes > > Hi > > I want to extract a row from a data.frame but I want that object to > be a vector . After trying some different ways I end up always with a > data.frame or with the wrong vector. Any pointers? > > x <- data.frame(a = factor(c('a',2,'b')), b = c(4,5,6)) > I want to get > "a" "4" > > I tried: > > as.vector(x[1,]) > a b > 1 a 4 > (resulting in a data.frame even after in my mind having coerced it > into a vector!) > > as.vector(c[1,], numeric='character') > [1] "2" "4" > (almost what I want, except that "2" instead of "a" - I guess this as > to do with levels and factors) > > Thanks for any help > > > R.Version() > $platform > [1] "powerpc-apple-darwin6.8" > > $arch > [1] "powerpc" > > $os > [1] "darwin6.8" > > $system > [1] "powerpc, darwin6.8" > > $status > [1] "" > > $major > [1] "2" > > $minor > [1] "0.1" > > $year > [1] "2004" > > $month > [1] "11" > > $day > [1] "15" > > $language > [1] "R" > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html > >
On Tue, 2004-11-23 at 16:27 +0000, Tiago R Magalhaes wrote:> Hi > > I want to extract a row from a data.frame but I want that object to > be a vector . After trying some different ways I end up always with a > data.frame or with the wrong vector. Any pointers? > > x <- data.frame(a = factor(c('a',2,'b')), b = c(4,5,6)) > I want to get > "a" "4" > > I tried: > > as.vector(x[1,]) > a b > 1 a 4 > (resulting in a data.frame even after in my mind having coerced it > into a vector!) > > as.vector(c[1,], numeric='character') > [1] "2" "4" > (almost what I want, except that "2" instead of "a" - I guess this as > to do with levels and factors) > > Thanks for any helpPart of the problem that you are having, as you seem to pick up, is that the first column in 'x' is a factor and you need to coerce it to a character. If you review the help for as.matrix, you will note in the details section: "as.matrix is a generic function. The method for data frames will convert any non-numeric/complex column into a character vector using format and so return a character matrix, except that all-logical data frames will be coerced to a logical matrix." Thus, one approach is:> as.matrix(x)[1, ]a b "a" "4" This coerces the data frame to a character matrix, which is then subsetted to the first row only. HTH, Marc Schwartz
Hi, as other already pointed out as.matrix is what you need. Just one comment: as.matrix(x[1,]) should be much faster for larger data frames compared to as.matrix(x)[1,] Best jan On Tue, 23 Nov 2004, Tiago R Magalhaes wrote:> Hi > > I want to extract a row from a data.frame but I want that object to > be a vector . After trying some different ways I end up always with a > data.frame or with the wrong vector. Any pointers? > > x <- data.frame(a = factor(c('a',2,'b')), b = c(4,5,6)) > I want to get > "a" "4" > > I tried: > > as.vector(x[1,]) > a b > 1 a 4 > (resulting in a data.frame even after in my mind having coerced it > into a vector!) > > as.vector(c[1,], numeric='character') > [1] "2" "4" > (almost what I want, except that "2" instead of "a" - I guess this as > to do with levels and factors) > > Thanks for any help > > > R.Version() > $platform > [1] "powerpc-apple-darwin6.8" > > $arch > [1] "powerpc" > > $os > [1] "darwin6.8" > > $system > [1] "powerpc, darwin6.8" > > $status > [1] "" > > $major > [1] "2" > > $minor > [1] "0.1" > > $year > [1] "2004" > > $month > [1] "11" > > $day > [1] "15" > > $language > [1] "R" > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html-- +----------------------------------------- Jan Goebel j g o e b e l @ d i w . d e DIW Berlin German Socio-Economic Panel Study (GSOEP) K??nigin-Luise-Str. 5 D-14195 Berlin -- Germany -- phone: 49 30 89789-377 +-----------------------------------------