0

I am trying to pull specific variables from a large data frame (A) into a new, smaller data frame (B) in R. Following the examples I can find online, I have been trying:

B <- [,c(A$var1, A$var2, A$var3)]

B <- [,c("A$var1", "A$var2", "A$var3")]

B[,c(A$var1, A$var2, A$var3)] 

B[,c("A$var1", "A$var2", "A$var3")]

All keep returning errors. Any help is appreciated.

Paul
  • 83
  • 6
  • 1
    It sounds like you want a `merge` or join if you are adding variables from one larger dataset to another smaller one. See here for lots of code that might be helpful: https://stackoverflow.com/questions/1299871/how-to-join-merge-data-frames-inner-outer-left-right – thelatemail May 04 '21 at 21:36
  • Welcome to SO, PaulBrancaleone! I agree with thelatemail that this sounds like a merge/join operation, but it's difficult to tell with what we know (which is not enough). Questions do better when they include sample data. Can you please add the output from `dput(head(A))` and same for `B`? If there are a lot of columns, we really only need to see a few (the ones above, for instance). There are other recommendations (some of which might apply) for reproducible questions here: https://stackoverflow.com/q/5963269, [mcve], and https://stackoverflow.com/tags/r/info. Thank you! – r2evans May 04 '21 at 21:46
  • Do you want to select specific columns from `A` to `B` ? `B <- A[,c('var1', 'var2', 'var3')]` . As others pointed output a reproducible example might help to understand what you are looking for. – Ronak Shah May 05 '21 at 03:31
  • @RonakShah This actually worked perfectly, thank you! I could tell my code was only slightly off but the examples I saw elsewhere confused me more. – Paul May 05 '21 at 04:03

0 Answers0