[R] please help! Extract the row to the new file by using if-statment
Rainer Schuermann
rainer.schuermann at gmx.net
Thu May 31 18:34:16 CEST 2012
Not sure whether I understand your data and objectives well enough but here is what I would do:
To make my life easier, I used x as a variable name. I'm not using attach().
You can extract your data with something like
y <- x[x$wrfta>= 255 | x$wrfta<= 65 & x$wrfrain == 0, ]
y <- y[!is.na(y[5]),]
> y
Date wrfRH wrfsolar wrfwindspeed wrfrain wrftd wrfta
1 21/10/2010 92.97 22.11 53.27 0 1546.3379 61.00853
2 22/10/2010 87.35 21.99 40.89 0 1300.4083 62.85352
7 27/10/2010 89.00 21.66 42.30 0 1060.4449 41.86858
9 29/10/2010 84.50 21.66 37.80 0 1015.8014 34.11626
10 30/10/2010 84.98 22.00 36.27 0 839.5041 43.44048
11 31/10/2010 84.40 22.40 33.44 0 742.5285 45.81573
12 1/11/2010 80.09 22.24 38.35 0 1157.9933 45.59035
13 2/11/2010 84.41 21.69 36.19 0 1075.2672 51.66310
14 3/11/2010 88.55 21.22 37.73 0 1163.2865 51.34180
18 7/11/2010 89.45 20.81 24.75 0 720.9882 57.76271
19 8/11/2010 85.82 20.96 28.63 0 790.5736 37.96772
20 9/11/2010 85.02 20.96 31.94 0 703.2994 40.62208
Does that help?
Rgds,
Rainer
By the way, it is better to provide the data in dput() format:
x <- structure(list(Date = structure(c(2L, 4L, 5L, 6L, 7L, 8L, 9L,
10L, 11L, 12L, 13L, 1L, 3L, 14L, 15L, 16L, 17L, 18L, 19L, 20L
), .Label = c("1/11/2010", "21/10/2010", "2/11/2010", "22/10/2010",
"23/10/2010", "24/10/2010", "25/10/2010", "26/10/2010", "27/10/2010",
"28/10/2010", "29/10/2010", "30/10/2010", "31/10/2010", "3/11/2010",
"4/11/2010", "5/11/2010", "6/11/2010", "7/11/2010", "8/11/2010",
"9/11/2010"), class = "factor"), wrfRH = c(92.97, 87.35, 88.38,
92.32, 93.42, 93.38, 89, NA, 84.5, 84.98, 84.4, 80.09, 84.41,
88.55, 90.58, 95.17, 95.2, 89.45, 85.82, 85.02), wrfsolar = c(22.11,
21.99, 21.71, 15.45, 21.59, 20.15, 21.66, NA, 21.66, 22, 22.4,
22.24, 21.69, 21.22, 2.88, 2.46, 11.18, 20.81, 20.96, 20.96),
wrfwindspeed = c(53.27, 40.89, 28.04, 22.38, 35.5, 42.58,
42.3, NA, 37.8, 36.27, 33.44, 38.35, 36.19, 37.73, 38.49,
32.22, 27.55, 24.75, 28.63, 31.94), wrfrain = c(0, 0, 0.01,
0.51, 0.52, 0.07, 0, NA, 0, 0, 0, 0, 0, 0, 0.56, 3.48, 0.84,
0, 0, 0), wrftd = c(1546.337861, 1300.408288, 1381.768284,
1113.90981, 868.4895334, 1404.722837, 1060.444918, 1109.596721,
1015.801383, 839.5041209, 742.5284832, 1157.99328, 1075.26719,
1163.286504, 1022.03364, 1065.735327, 1027.066675, 720.9881913,
790.5735604, 703.2993511), wrfta = c(61.00852664, 62.85352227,
54.80594493, 39.46573663, 28.42952321, 40.29300856, 41.86858345,
39.84995092, 34.11625725, 43.44047866, 45.81572847, 45.59035293,
51.66310159, 51.34179935, 57.74352136, 57.7734991, 54.40282225,
57.76270824, 37.96771725, 40.62208274), cat = c(NA, NA, 1,
1, 1, 1, NA, NA, NA, NA, NA, NA, NA, NA, 1, 1, 1, NA, NA,
NA)), .Names = c("Date", "wrfRH", "wrfsolar", "wrfwindspeed",
"wrfrain", "wrftd", "wrfta", "cat"), row.names = c(NA, -20L), class = "data.frame")
On Thursday 31 May 2012 07:55:01 pigpigmeow wrote:
> 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.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help
mailing list