1

I'm trying to use a for loop to pull subsets of data out of a dataframe with R.

I have a little vector to hold all the possible occurences of the names in that column

meter_class<-c("one_s_120","nine_s_120", "nine_s_480","fortyfive_s_120", "fortyfive_s_480")

Whenever I try to address it by index reference, it fails. Either nothing in the data subset survives (NULLs everywhere), or R complains about not passing the right argument by using meter_class[1]

attach(meter_class[1])
Error in attach(meter_class[1]) : file 'one_s_120' not found
subset(cal, cal$Form==as.character(meter_class[1]))
[1] Test     Amps     Type     Accuracy Voltage  Form 
<0 rows> (or 0-length row.names)

Also, here's the output of dput on the datafram cal:

    structure(list(Test = structure(c(3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L), .Label = c("Burst", "ESD", "Inspection", 
"Surge"), class = "factor"), Amps = c(15, 15, 1.5, 2.5, 2.5, 
0.25, 2.5, 2.5, 0.25, 2.5, 2.5, 0.25, 2.5, 2.5, 0.25, 15, 15, 
1.5, 2.5, 2.5, 0.25, 2.5, 2.5, 0.25, 2.5, 2.5, 0.25, 2.5, 2.5, 
0.25, 15, 15, 1.5, 2.5, 2.5, 0.25, 2.5, 2.5, 0.25, 2.5, 2.5, 
0.25, 2.5, 2.5, 0.25, 15, 15, 1.5, 2.5, 2.5, 0.25, 2.5, 2.5, 
0.25, 2.5, 2.5, 0.25, 2.5, 2.5, 0.25), Type = structure(c(2L, 
1L, 3L, 2L, 1L, 3L, 2L, 1L, 3L, 2L, 1L, 3L, 2L, 1L, 3L, 2L, 1L, 
3L, 2L, 1L, 3L, 2L, 1L, 3L, 2L, 1L, 3L, 2L, 1L, 3L, 2L, 1L, 3L, 
2L, 1L, 3L, 2L, 1L, 3L, 2L, 1L, 3L, 2L, 1L, 3L, 2L, 1L, 3L, 2L, 
1L, 3L, 2L, 1L, 3L, 2L, 1L, 3L, 2L, 1L, 3L), .Label = c("Lag - 0.5", 
"Unity - Full", "Unity - Light"), class = "factor"), Accuracy = c(-0.011, 
0.012, 0.027, 0.033, 0.076, 0.006, 0.052, 0.046, -0.016, 0.021, 
0.008, 0.023, 0.034, 0.036, 0.038, 0.002, 0.012, 0.097, 0.055, 
0.093, 0.033, 0.068, 0.048, -0.016, 0.042, 0.03, 0.035, 0.041, 
0.024, 0.027, 0.004, -0.012, 0.002, 0.038, 0.084, 0.015, 0.049, 
0.045, -0.009, 0.025, 0.002, 0.029, 0.03, 0.032, 0.064, 0.011, 
0.024, 0.033, 0.054, 0.085, 0.027, 0.071, 0.059, 0.01, 0.051, 
0.012, 0.051, 0.048, 0.04, 0.051), Voltage = c(120, 120, 120, 
120, 120, 120, 480, 480, 480, 120, 120, 120, 480, 480, 480, 120, 
120, 120, 120, 120, 120, 480, 480, 480, 120, 120, 120, 480, 480, 
480, 120, 120, 120, 120, 120, 120, 480, 480, 480, 120, 120, 120, 
480, 480, 480, 120, 120, 120, 120, 120, 120, 480, 480, 480, 120, 
120, 120, 480, 480, 480), Form = structure(c(3L, 3L, 3L, 2L, 
2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 3L, 3L, 2L, 2L, 
2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 3L, 3L, 2L, 2L, 2L, 
2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 3L, 3L, 2L, 2L, 2L, 2L, 
2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("45S", "9S", "i210plus"
), class = "factor")), .Names = c("Test", "Amps", "Type", "Accuracy", 
"Voltage", "Form"), class = "data.frame", row.names = c(NA, -60L
))

I know this is a simple thing to do if you know how to do it...Can anyone light the way?

Thanks!

testname123
  • 1,061
  • 3
  • 20
  • 43
  • 2
    Hi and welcome to SO! It so much easier for people to help you with your problem if you post a [minimal, reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610). Also, post any error message - they are often quite informative. Finally, avoid using `attach`. See e.g. `?attach`,"Good practice", and several threads in forums about potentially unpleasant surprises caused by `attach`. – Henrik Nov 25 '13 at 19:00
  • Thanks for the response! I took your advice and updated the code to include error messages. – testname123 Nov 25 '13 at 19:09
  • The post has been updated. Thanks for the direction towards getting help. – testname123 Nov 25 '13 at 19:44
  • 1
    Your column `Form` in `cal` only has two entries: `i210plus 9S` and `45S`. Neither of them are in your vector `meter_class`. However, the syntax you've used is almost correct (avoid using `attach` at all costs!) `cal[cal$Form %in% meter_class, ]`. – Justin Nov 25 '13 at 19:49
  • Ok, I see some of the problem. I went ahead and changed meter_class to c("i210plus","9S","45S"). So, without know exactly what Justin's comment of cal[cal$Form %in% meter_class, ] does with the %in% term, let me ask you this: How would you use that syntax to select all the say, 9S class meters that were run at 120 and not 480? Can you somehow slip an AND in there? :) Thanks...this site is beyond helpful and informative! – testname123 Nov 25 '13 at 20:43
  • This is really basic R stuff. Please read some introductory text, such [this](http://cran.r-project.org/doc/manuals/R-intro.html), or [this](http://www.burns-stat.com/documents/tutorials/impatient-r/). [This section is especially relevant](http://cran.r-project.org/doc/manuals/R-intro.html#Index-vectors). – Henrik Nov 25 '13 at 21:04
  • More suggsted reading: http://adv-r.had.co.nz/Subsetting.html – Henrik Nov 25 '13 at 21:15
  • @user3033594: did this post solve your problem? It helps keep us interested if you indicate that the answer is acceptable and helps others who might look up your question in future to see that the answer given was suitable. – Matt Weller Jan 14 '14 at 01:25

1 Answers1

1

It seems that none of the values of "meter_class" are represented in "Form" in your data frame.

unique(df$Form)
# [1] i210plus 9S       45S

meter_class %in% unique(df$Form)
# [1] FALSE FALSE FALSE FALSE FALSE

Just try two forms of subsetting, using values of "Form" actually present in the data:

subset(df, Form == "9S")
df[df$Form == "9S", ]

I also note that you wish to "pull subsets of data out of a dataframe". Not knowing the full story and your objectives of doing so, but please note that there are loads of functions that allow you to perform calculations, plotting, or whatever, on subsets of your data.

Update following comment
You can subset a data frame by combining logical conditions with logical operators (see e.g. ?Extract, ?&)

meter_class <- c("i210plus", "9S", "45S")
df[df$Form == "9S" & df$Voltage == 120, ]
# or
subset(df, Form == "9S" & Voltage == 120)
Henrik
  • 65,555
  • 14
  • 143
  • 159