-1

I have a data frame df, which is as follows. The left-hand side is the row names and the first column is the corresponding values.

              v1   
 a177:a1297  0.1
 a177:a842   0.2
 a177:a796   0.4
 a24:a1437   0.3
 a24:a1256   0.6
 a24:a762    0.7

I have a vector of characters which are the row names of df. The vector is dfnames

  str(dfnames)
  chr [1:5] "a177:a1297" "a177:a842" "a177:a796" "a24:a1437" "a24:a1256" "a24:a762"

If I extract one value based on a specific row name, it would be:

    df["a177:a1297",]
   [1]  0.1 

Now I want to extract all the values of df based on dfnames

  df[dfnames,]
  [1] NA NA NA NA NA NA

Could anyone tell me how to extract the values of df based on a vector that contains the row names of df?

Uwe
  • 41,420
  • 11
  • 90
  • 134
user5802211
  • 197
  • 1
  • 9

1 Answers1

3

You can do:

df[which(row.names(df) %in% dfnames),]
Bea
  • 1,110
  • 12
  • 20