[R] please help! Extract the row to the new file by using if-statment

pigpigmeow glorykwok at hotmail.com
Thu May 31 16:55:01 CEST 2012


Dear all,
I find some troubles about how to extact the row from csv. file by using
if-statement condition.
I want to extract the row if the rainfall is greater than the mean of
rainfall and using the wrfta divided into 3 groups
that's 
rainfall greater than mean -> group A ( create file group A_rain) 
                                                -> groupB ( create file
group B_rain) 
                                                -> groupC ( create file
group C_rain) 
rainfall less than mean -> group A ( create file group A_norain) 
                                                -> groupB ( create file
group B_norain) 
                                                -> groupC ( create file
group C_norain) 
my csv. file is ..
Date	                  wrfRH	wrfsolar	wrfwindspeed	wrfrain	wrftd	wrfta
21/10/2010	92.97	22.11	53.27	0	1546.337861	61.00852664
22/10/2010	87.35	21.99	40.89	0	1300.408288	62.85352227
23/10/2010	88.38	21.71	28.04	0.01	1381.768284	54.80594493
24/10/2010	92.32	15.45	22.38	0.51	1113.90981	39.46573663
25/10/2010	93.42	21.59	35.5	0.52	868.4895334	28.42952321
26/10/2010	93.38	20.15	42.58	0.07	1404.722837	40.29300856
27/10/2010	89	21.66	42.3	0	1060.444918	41.86858345
28/10/2010	NA	NA	NA	NA	1109.596721	39.84995092
29/10/2010	84.5	21.66	37.8	0	1015.801383	34.11625725
30/10/2010	84.98	22	36.27	0	839.5041209	43.44047866
31/10/2010	84.4	22.4	33.44	0	742.5284832	45.81572847
1/11/2010  	80.09	22.24	38.35	0	1157.99328	45.59035293
2/11/2010 	84.41	21.69	36.19	0	1075.26719	51.66310159
3/11/2010 	88.55	21.22	37.73	0	1163.286504	51.34179935
4/11/2010 	90.58	2.88	38.49	0.56	1022.03364	57.74352136
5/11/2010	                   95.17	2.46	32.22	3.48	1065.735327	57.7734991
6/11/2010 	95.2	11.18	27.55	0.84	1027.066675	54.40282225
7/11/2010	                  89.45	20.81	24.75	0	720.9881913	57.76270824
8/11/2010  	85.82	20.96	28.63	0	790.5735604	37.96771725
9/11/2010 	85.02	20.96	31.94	0	703.2993511	40.62208274

my script is .....
#Import data
wrfJJA_UTC06<-read.csv("JJA_UTC06_ALL.csv", header =T,sep=",")
attach(wrfJJA_UTC06)

	if(wrfrain < a) 
		groupA_norain<- new[wrfta>= 255 | wrfta<= 65,]
		groupB_norain<- new[wrfta>= 65 & wrfta<= 180,]
		groupC_norain<- new[wrfta>= 180 & wrfta<= 255,]
	

	else
		groupA_rain<- new[wrfta>= 255 | wrfta<= 65,]
		groupB_rain<- new[wrfta>= 65 & wrfta<= 180,]
		groupC_rain<- new[wrfta>= 180 & wrfta<= 255,]

#save as ...
write.csv(groupA_norain,"groupA_norain.csv")
write.csv(groupB_norain,"groupB_norain.csv")
.....
	

however, it gets error message. what wrong?
Warning message:
In if (n_wrfrain < a) groupA_norain <- new[n_wrfta >= 255 | n_wrfta <=  :
  the condition has length > 1 and only the first element will be used

my data wrfrain contains NA. what can I do?!
please help!

--
View this message in context: http://r.789695.n4.nabble.com/please-help-Extract-the-row-to-the-new-file-by-using-if-statment-tp4631957.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list