0
Site   Treatment    Month    repli
W2         0          0      2
W3         0          0      1 
W7         0          0      3
W12        0          0      4
W2         0          1      2
W3         0          1      1
W7         0          1      3 
W12        0          1      4
W2         0          3      2
W3         0          3      1
W7         0          3      3 
W12        0          3      4
W2         0          4      2
W3         0          4      1
W7         0          4      3 
W12        0          4      4

If I use

for(i in 1:max(data$Repli)){
s1=subset(data,data$Repli==i)

I get the last row (w12 one)

Site      Treatment   Month    repli
W12           0         0       4
W12           0         1       4
W12           0         3       4
W12           0         5       4

My question is how to select other rows? What would be my code when I would like to select W2, W3 or W7?

Thank you!

I have to use the same script as i from the below script is being used elsewhere

for(i in 1:max(Cs$Repli)){
  s1=subset(Cs,Cs$Repli==i)  
Mr Good News
  • 121
  • 1
  • 3
  • 15
  • I think your current code should work OK for selecting each subset, but they will only be selected during the current loop iteration. As soon as you get to the next iteration and `s1` is overwritten, you don't have that subset any more. If you want to save all subsets you could use `subsets = split(data, data$repli)` – Marius Jan 16 '18 at 05:52
  • @Marius Thank you. However, (i) from above script for(i in 1:max(data$Repli)){ s1=subset(data,data$Repli==i) is being used elsewhere too. So, I have to use to same code for other rows too. It is okay if it is over written. – Mr Good News Jan 16 '18 at 06:31

1 Answers1

0

Use %in%:

data[data$Site %in% c("W2", "W3", "W7"), ]
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • Beigeleisen Thank you sir for quick reply. – Mr Good News Jan 16 '18 at 06:02
  • I marked it. However there was a problem. The script that I was using for(i in 1:max(Cs$Repli)){s1=subset(Cs,Cs$Repli==i). The i from above script is used else where too. So I cant use %in%. Since your script answered my specific question I have marked it. Thanks! – Mr Good News Jan 16 '18 at 06:46