Convert a row of a data frame to vector

前端 未结 6 620
予麋鹿
予麋鹿 2020-12-04 09:05

I want to create a vector out of a row of a data frame. But I don\'t want to have to row and column names. I tried several things... but had no luck.

This is my data

6条回答
  •  不知归路
    2020-12-04 09:37

    When you extract a single row from a data frame you get a one-row data frame. Convert it to a numeric vector:

    as.numeric(df[1,])
    

    As @Roland suggests, unlist(df[1,]) will convert the one-row data frame to a numeric vector without dropping the names. Therefore unname(unlist(df[1,])) is another, slightly more explicit way to get to the same result.

    As @Josh comments below, if you have a not-completely-numeric (alphabetic, factor, mixed ...) data frame, you need as.character(df[1,]) instead.

提交回复
热议问题