-1

I would appreciate your suggestions on subsetting a data frame. Let's consider an example data frame df:

dd <- c(1,2,3)
rows <- c("A1","A2","A3")
columns <- c("B1","B2","B3")
numbers <- c(400, 500, 600)    
df <- dataframe(dd, rows, columns, numbers)

and a vector : test_rows <-c("A1","A3")

How could I subset the data frame df function of vector test_rows in such a way that only the row of data frame df (df$rows) that match the elements of test_rows ("A1" and "A3") are listed?

josliber
  • 41,865
  • 12
  • 88
  • 126
Bogdan
  • 113
  • 8
  • 1
    You can use the function "subset" – Michele Usuelli Jun 08 '15 at 22:53
  • 1
    http://www.ats.ucla.edu/stat/r/faq/subset_R.htm. First result for Google query "R subset data frame". I may be not giving you adequate benefit-of-the-doubt, but your question "does not show any research effort." – Jubbles Jun 08 '15 at 23:47

2 Answers2

5

Here is your answer:

df[df$rows %in% test_rows,]

btw, df is a name of a built in function in R so it is better not to use it as a name for variables.

Michal
  • 1,663
  • 7
  • 27
  • 47
  • Where is `df`a reserved word? I saw here: https://stat.ethz.ch/R-manual/R-devel/library/base/html/Reserved.html but there's not `df`. – SabDeM Jun 08 '15 at 23:32
  • 2
    df is a function. This is what happens when I type `df`: `function (x, df1, df2, ncp, log = FALSE)` – Michal Jun 08 '15 at 23:34
  • You corrected well because a reserved word is a different thing respect to the name of a function. Try for example to assign a value to `for` of `while` words. Anyway, you can assign values in any case with `"` and take them with `get` function or just assign with backticks ````. – SabDeM Jun 08 '15 at 23:38
  • 1
    people tend to use `df` for a lot of examples in stack overflow and it has become a real pet peeve of mine since after helping someone, `df` always shows up when I check `conflicts()`. – Michal Jun 08 '15 at 23:44
2

Just in case your data set is very large and you want a faster solution here is the dplyr code to achieve the same result:

df %>% filter(rows %in% test_rows)
SabDeM
  • 6,638
  • 2
  • 22
  • 37