Cómo obtener fila de datos R.marco


Tengo un dato.marco con cabeceras de columna.

¿Cómo puedo obtener una fila específica de los datos.frame como una lista (con los encabezados de columna como claves para la lista)?

Específicamente, mis datos.marco es

      A    B    C
    1 5    4.25 4.5
    2 3.5  4    2.5
    3 3.25 4    4
    4 4.25 4.5  2.25
    5 1.5  4.5  3

Y quiero obtener una fila que es el equivalente de

> c(a=5, b=4.25, c=4.5)
  a   b   c 
5.0 4.25 4.5 
Author: Hack-R, 2009-08-13

4 answers

x[r,]

Donde r es la fila que le interesa. Prueba esto, por ejemplo:

#Add your data
x <- structure(list(A = c(5,    3.5, 3.25, 4.25,  1.5 ), 
                    B = c(4.25, 4,   4,    4.5,   4.5 ),
                    C = c(4.5,  2.5, 4,    2.25,  3   )
               ),
               .Names    = c("A", "B", "C"),
               class     = "data.frame",
               row.names = c(NA, -5L)
     )

#The vector your result should match
y<-c(A=5, B=4.25, C=4.5)

#Test that the items in the row match the vector you wanted
x[1,]==y

Esta página (de este útil sitio) tiene buena información sobre indexación como esta.

 106
Author: Matt Parker,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-05-12 13:05:58

La indexación lógica es muy R-ish. Try:

 x[ x$A ==5 & x$B==4.25 & x$C==4.5 , ] 

O:

subset( x, A ==5 & B==4.25 & C==4.5 )
 11
Author: 42-,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-05-11 17:31:58

Intenta:

> d <- data.frame(a=1:3, b=4:6, c=7:9)

> d
  a b c
1 1 4 7
2 2 5 8
3 3 6 9

> d[1, ]
  a b c
1 1 4 7

> d[1, ]['a']
  a
1 1
 5
Author: ars,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2009-08-13 02:22:34

Si no conoce el número de fila, pero conoce algunos valores, puede usar subconjunto

x <- structure(list(A = c(5,    3.5, 3.25, 4.25,  1.5 ), 
                    B = c(4.25, 4,   4,    4.5,   4.5 ),
                    C = c(4.5,  2.5, 4,    2.25,  3   )
               ),
               .Names    = c("A", "B", "C"),
               class     = "data.frame",
               row.names = c(NA, -5L)
     )

subset(x, A ==5 & B==4.25 & C==4.5)
 4
Author: Thierry,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-05-12 13:07:14