Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in R Programming by (7.3k points)

I have a data.frame with column headers.

How can I get a specific row from the data.frame as a list (with the column headers as keys for the list)?

Specifically, my data.frame is

      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

And I want to get a row that's the equivalent of

> c(a=5, b=4.25, c=4.5)

  a   b   c 

5.0 4.25 4.5 

1 Answer

0 votes
by

To get a row from a data frame, you can do the following:

data_frame_name[row_name, ]

For example:

x <- data.frame(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))

x[1,]

  A    B   C

1 5 4.25 4.5

Browse Categories

...