[R] grouping data

Steve Hong
Fri, 04 Mar 2011 21:39:40 -0000

Hi R-list,

I have a data set with plot locations and observations and want to label
them based on locations.  For example, I have GPS information (x and y) as
follows:

> x
  [1] -87.85092 -87.85092 -87.85092 -87.85093 -87.85093 -87.85093 -87.85094
  [8] -87.85094 -87.85094 -87.85096 -87.85095 -87.85095 -87.85095 -87.85096
 [15] -87.85096 -87.85096 -87.85096 -87.85088 -87.85088 -87.85087 -87.85087
 [22] -87.85087 -87.85087 -87.85086 -87.85086 -87.85086 -87.85085 -87.85086
 [29] -87.85085 -87.85085 -87.85084 -87.85084 -87.85084 -87.85084 -87.85075
 [36] -87.85075 -87.85076 -87.85076 -87.85077 -87.85076 -87.85076 -87.85076
 [43] -87.85077 -87.85077 -87.85077 -87.85077 -87.85077 -87.85077 -87.85070
 [50] -87.85072 -87.85073 -87.85075 -87.85078 -87.85079 -87.85082 -87.85084
 [57] -87.85077 -87.85078 -87.85078 -87.85078 -87.85078 -87.85078 -87.85079
 [64] -87.85079 -87.85080 -87.85080 -87.85071 -87.85071 -87.85071 -87.85070
 [71] -87.85071 -87.85079 -87.85071 -87.85070 -87.85070 -87.85069 -87.85069
 [78] -87.85069 -87.85069 -87.85068 -87.85068 -87.85068 -87.85067 -87.85059
 [85] -87.85060 -87.85060 -87.85060 -87.85061 -87.85061 -87.85061 -87.85061
 [92] -87.85061 -87.85062 -87.85062 -87.85062 -87.85062 -87.85063 -87.85063
 [99] -87.85063 -87.85055 -87.85055 -87.85055 -87.85054 -87.85054 -87.85053
[106] -87.85053 -87.85053 -87.85053 -87.85053 -87.85052 -87.85052 -87.85052
[113] -87.85052 -87.85051 -87.85051 -87.85043 -87.85043 -87.85044 -87.85044
[120] -87.85044 -87.85045 -87.85045 -87.85045 -87.85045 -87.85046 -87.85046
[127] -87.85046 -87.85046 -87.85047 -87.85047 -87.85039 -87.85039 -87.85038
[134] -87.85038 -87.85038 -87.85037 -87.85037 -87.85037 -87.85037 -87.85036
[141] -87.85036 -87.85036 -87.85035 -87.85035 -87.85035 -87.85027 -87.85027
[148] -87.85027 -87.85027 -87.85028 -87.85028 -87.85028 -87.85029 -87.85029
[155] -87.85029 -87.85029 -87.85029 -87.85030 -87.85030 -87.85030 -87.85022
[162] -87.85022 -87.85022 -87.85021 -87.85021 -87.85021 -87.85020 -87.85020
[169] -87.85020 -87.85020 -87.85019 -87.85019 -87.85019 -87.85019 -87.85011
[176] -87.85011 -87.85011 -87.85011 -87.85012 -87.85012 -87.85012 -87.85012
[183] -87.85013 -87.85013 -87.85013 -87.85014 -87.85014 -87.85014 -87.85006
[190] -87.85006 -87.85006 -87.85005 -87.85005 -87.85004 -87.85004 -87.85004
[197] -87.85004 -87.85003 -87.85003 -87.85003 -87.85002 -87.85003 -87.84994
[204] -87.84994 -87.84995 -87.84995 -87.84995 -87.84995 -87.84996 -87.84996
[211] -87.84996 -87.84996 -87.84996 -87.84996 -87.84996 -87.84996 -87.84996
[218] -87.84996 -87.84996 -87.84996 -87.84996 -87.84990 -87.84991 -87.84993
[225] -87.84995 -87.84998 -87.84999 -87.85001 -87.85003 -87.84996 -87.84998
[232] -87.84997 -87.84998 -87.84989 -87.84990 -87.84989 -87.84989 -87.84988
[239] -87.84988 -87.84988 -87.84988 -87.84988 -87.84987 -87.84987 -87.84987
[246] -87.84987 -87.84978 -87.84978 -87.84979 -87.84979 -87.84979 -87.84979
[253] -87.84979 -87.84980 -87.84980 -87.84981 -87.84980 -87.84981 -87.84981
[260] -87.84973 -87.84973 -87.84973 -87.84972 -87.84972 -87.84972 -87.84971
[267] -87.84971 -87.84971 -87.84970 -87.84970 -87.84970 -87.84963 -87.84963
[274] -87.84963 -87.84963 -87.84963 -87.84964 -87.84964 -87.84965 -87.84964
[281] -87.84964 -87.84965 -87.84957 -87.84957 -87.84956 -87.84956 -87.84958
[288] -87.84958
> y
  [1] 33.90342 33.90335 33.90328 33.90321 33.90314 33.90308 33.90301
33.90294
  [9] 33.90287 33.90280 33.90274 33.90267 33.90260 33.90253 33.90246
33.90240
 [17] 33.90233 33.90232 33.90239 33.90245 33.90252 33.90259 33.90266
33.90273
 [25] 33.90279 33.90286 33.90293 33.90300 33.90307 33.90314 33.90321
33.90327
 [33] 33.90334 33.90339 33.90337 33.90335 33.90328 33.90321 33.90319
33.90318
 [41] 33.90317 33.90316 33.90315 33.90313 33.90312 33.90310 33.90309
33.90307
 [49] 33.90314 33.90314 33.90314 33.90314 33.90314 33.90314 33.90314
33.90314
 [57] 33.90300 33.90294 33.90287 33.90280 33.90273 33.90266 33.90252
33.90245
 [65] 33.90239 33.90232 33.90231 33.90237 33.90245 33.90251 33.90258
33.90259
 [73] 33.90265 33.90272 33.90279 33.90286 33.90292 33.90299 33.90306
33.90313
 [81] 33.90320 33.90326 33.90334 33.90332 33.90327 33.90320 33.90314
33.90307
 [89] 33.90300 33.90293 33.90286 33.90279 33.90272 33.90265 33.90258
33.90252
 [97] 33.90245 33.90238 33.90231 33.90231 33.90237 33.90243 33.90250
33.90257
[105] 33.90264 33.90271 33.90278 33.90285 33.90292 33.90298 33.90306
33.90312
[113] 33.90319 33.90326 33.90329 33.90326 33.90319 33.90312 33.90306
33.90299
[121] 33.90292 33.90286 33.90279 33.90272 33.90265 33.90258 33.90251
33.90245
[129] 33.90237 33.90231 33.90230 33.90236 33.90243 33.90250 33.90257
33.90264
[137] 33.90271 33.90277 33.90284 33.90291 33.90298 33.90305 33.90311
33.90319
[145] 33.90325 33.90323 33.90319 33.90312 33.90305 33.90299 33.90291
33.90285
[153] 33.90278 33.90272 33.90264 33.90257 33.90250 33.90243 33.90237
33.90230
[161] 33.90229 33.90235 33.90243 33.90250 33.90256 33.90263 33.90270
33.90277
[169] 33.90283 33.90290 33.90297 33.90304 33.90311 33.90318 33.90318
33.90311
[177] 33.90304 33.90298 33.90291 33.90284 33.90277 33.90270 33.90263
33.90256
[185] 33.90250 33.90243 33.90236 33.90229 33.90229 33.90235 33.90242
33.90249
[193] 33.90256 33.90262 33.90269 33.90276 33.90283 33.90290 33.90297
33.90303
[201] 33.90311 33.90316 33.90315 33.90311 33.90304 33.90297 33.90290
33.90283
[209] 33.90270 33.90263 33.90262 33.90260 33.90259 33.90258 33.90257
33.90256
[217] 33.90255 33.90254 33.90253 33.90251 33.90250 33.90255 33.90255
33.90255
[225] 33.90256 33.90256 33.90256 33.90256 33.90256 33.90249 33.90243
33.90236
[233] 33.90229 33.90228 33.90235 33.90242 33.90248 33.90255 33.90262
33.90269
[241] 33.90276 33.90283 33.90289 33.90296 33.90304 33.90310 33.90310
33.90304
[249] 33.90297 33.90290 33.90284 33.90276 33.90269 33.90263 33.90256
33.90249
[257] 33.90242 33.90235 33.90228 33.90227 33.90234 33.90241 33.90247
33.90255
[265] 33.90261 33.90268 33.90275 33.90282 33.90289 33.90296 33.90303
33.90297
[273] 33.90289 33.90283 33.90275 33.90269 33.90262 33.90255 33.90248
33.90242
[281] 33.90235 33.90228 33.90227 33.90234 33.90241 33.90247 33.90254
33.90261

I mapped the locations of sampling plots based on x and y.

> plot(x,y)
I made arbitrarily 'fake' data with certain means and standard
deviations to
generate regression line as follows:
> (xsim <- sort(rnorm(30, xmn, xsd)))
 [1] -87.85130 -87.85125 -87.85093 -87.85092 -87.85081 -87.85074 -87.85070
 [8] -87.85070 -87.85060 -87.85059 -87.85053 -87.85046 -87.85046 -87.85042
[15] -87.85037 -87.85028 -87.85026 -87.85026 -87.85025 -87.85023 -87.85019
[22] -87.85015 -87.85007 -87.85003 -87.85000 -87.84970 -87.84957 -87.84957
[29] -87.84947 -87.84941
> (ysim <- sort(rnorm(30, ymn, ysd)))
 [1] 33.90227 33.90237 33.90239 33.90240 33.90243 33.90248 33.90249 33.90251
 [9] 33.90251 33.90262 33.90262 33.90266 33.90267 33.90268 33.90272 33.90274
[17] 33.90274 33.90278 33.90283 33.90286 33.90286 33.90294 33.90295 33.90301
[25] 33.90304 33.90316 33.90317 33.90318 33.90319
33.90321
> (fm1 <- lm(ysim~xsim))
Call:
lm(formula = ysim ~ xsim)
Coefficients:
(Intercept)         xsim
    82.9996       0.5589

I overlapped fitted line on the plot.

> abline(fm1)
My question is:
As you can see in the plot, how can I label (or re-group) those in upper
diagonal as (say) 'A' and the others in lower diagonal as 'B'?

Thanks a lot in advance!!!

Steve

	[[alternative HTML version deleted]]