-1

I'm trying to get the data from column one that matches with column 2 but only on the "B" values. Need to somehow make the true values a list. Need this to repeat for 50,000 rows. Around 37,000 of them are true. I'm incredibly new to this so any help would be nice.

Data <- data.frame(
X = sample(1:10),
Y = sample(c("B", "W"), 10, replace = TRUE)
)
Count <- 1
If(data[count,2] == "B") {
  List <- list(data[count,1]
  Count <- count + 1
#I'm not sure what to use to repeat I just put
Repeat
} else {
Count <- count + 1
Repeat
}

End result should be a list() of only column one data. In this if rows 1-5 had "B" I want the column one numbers from that.

RHertel
  • 23,412
  • 5
  • 38
  • 64
BroMan
  • 3
  • 4
  • Could you add a minimal example of what you try to do, including the expected outcome?! – Cleb Jun 03 '16 at 22:48
  • 1
    Please edit your question, giving a simple example and the expected outcome. In the meantime, it is really hard to understand what you are trying to do. – pbahr Jun 03 '16 at 23:54
  • We need to have your data (at least its structure) and have a reproducible code to run and see where you're stuck. Please check this [thread](http://stackoverflow.com/a/5963610/2864184) for a guideline. – pbahr Jun 04 '16 at 01:23
  • 1
    What would the result look like? – Roman Luštrik Jun 04 '16 at 09:05
  • Should be a list with the values from column one. Only the ones that match "B" in column 2. Don't know how that would look in code. – BroMan Jun 04 '16 at 12:10

1 Answers1

0

Not sure if I understood correctly what you're looking for, but from the comments I would assume that this might help:

setNames(data.frame(Data[1][Data[2]=="B"]), "selected")
#  selected
#1        2
#2        5
#3        7
#4        6

No loop needed.


data

Data <- structure(list(X = c(10L, 4L, 9L, 8L, 3L, 2L, 5L, 1L, 7L, 6L), 
              Y = structure(c(2L, 2L, 2L, 2L, 2L, 1L, 1L, 2L, 1L, 1L), 
             .Label = c("B", "W"), class = "factor")), 
             .Names = c("X", "Y"), row.names = c(NA, -10L), 
              class = "data.frame")
RHertel
  • 23,412
  • 5
  • 38
  • 64