Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
148 views
in Technique[技术] by (71.8m points)

r - How to subset the randomly repeating X and Y address with values

I have data frame with 3 columns and more than 200000 rows. The first 2 columns are the x and y address of 3 column (values) and each address is repeating 365 times with different values. I have to extract each x,y address with it 365 values saperately.

            X      Y     Value
3297   33.625184 70.875   0.04
3298   33.875184 70.875   0.02
3299   34.125184 70.875   0.01
3300   34.375184 70.875   0.03
3301   34.625184 70.875   0.09
3302   34.875184 70.875   0.14
3303   35.125184 70.875   0.17
3304   35.375184 70.875   0.12
3305   35.625184 70.875   0.13
3306   35.875184 70.875   0.11
3307   36.125184 70.875   0.12
3308   36.375184 70.875   0.11
3309   36.625184 70.875   0.07
3310   36.875184 70.875   0.08
3311   37.125184 70.875   0.13
3312   37.375184 70.875   6.61
3313   33.125185 70.875   3.15
3314   33.375185 70.875   3.72
3315   33.625185 70.875   4.24
3316   33.875185 70.875   3.20
3317   34.125185 70.875   2.83
3318   34.375185 70.875   3.53
3319   34.625185 70.875   4.24
3320   34.875185 70.875   3.81
3321   35.125185 70.875   1.50
3322   35.375185 70.875   0.51
3323   35.625185 70.875   0.01
3324   35.875185 70.875   0.00
3325   36.125185 70.875   0.01
3326   36.375185 70.875   0.13
3327   36.625185 70.875   0.18
3328   36.875185 70.875   0.22
3329   37.125185 70.875   0.21
3330   37.375185 70.875   0.00
3331   33.125186 70.875   0.00
3332   33.375186 70.875   0.00
3333   33.625186 70.875   0.00

I have tried $ command but could not work. Any help will be highly appreciable.

The expected output will be like this: x,y(1:365) values.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Assuming you want to subset 365 similar values of X and Y in one group. You can split the data frame w.r.t X and Y using,

split(df, list(df$X, df$Y))

For example in mtcars dataset if you need to split it according to cyl and am column then

split(mtcars,list(mtcars$cyl,mtcars$am))

which would give you output as

#$`4.0`
#          mpg cyl  disp hp drat    wt  qsec vs am gear carb
#Merc 240D     24.4   4 146.7 62 3.69 3.190 20.00  1  0    4    2
#Merc 230      22.8   4 140.8 95 3.92 3.150 22.90  1  0    4    2
#Toyota Corona 21.5   4 120.1 97 3.70 2.465 20.01  1  0    3    1

#$`6.0`
#            mpg cyl  disp  hp drat    wt  qsec vs am gear carb
#Hornet 4 Drive 21.4   6 258.0 110 3.08 3.215 19.44  1  0    3    1
#Valiant        18.1   6 225.0 105 2.76 3.460 20.22  1  0    3    1
#Merc 280       19.2   6 167.6 123 3.92 3.440 18.30  1  0    4    4
#Merc 280C      17.8   6 167.6 123 3.92 3.440 18.90  1  0    4    4

#$`8.0`
#                 mpg cyl  disp  hp drat    wt  qsec vs am gear carb
#Hornet Sportabout   18.7   8 360.0 175 3.15 3.440 17.02  0  0    3    2
#Duster 360          14.3   8 360.0 245 3.21 3.570 15.84  0  0    3    4
#Merc 450SE          16.4   8 275.8 180 3.07 4.070 17.40  0  0    3    3
#Merc 450SL          17.3   8 275.8 180 3.07 3.730 17.60  0  0    3    3
#Merc 450SLC         15.2   8 275.8 180 3.07 3.780 18.00  0  0    3    3
#Cadillac Fleetwood  10.4   8 472.0 205 2.93 5.250 17.98  0  0    3    4
#Lincoln Continental 10.4   8 460.0 215 3.00 5.424 17.82  0  0    3    4
#Chrysler Imperial   14.7   8 440.0 230 3.23 5.345 17.42  0  0    3    4
#Dodge Challenger    15.5   8 318.0 150 2.76 3.520 16.87  0  0    3    2
#AMC Javelin         15.2   8 304.0 150 3.15 3.435 17.30  0  0    3    2
#Camaro Z28          13.3   8 350.0 245 3.73 3.840 15.41  0  0    3    4
#Pontiac Firebird    19.2   8 400.0 175 3.08 3.845 17.05  0  0    3    2

#$`4.1`
#            mpg cyl  disp  hp drat    wt  qsec vs am gear carb
#Datsun 710     22.8   4 108.0  93 3.85 2.320 18.61  1  1    4    1
#Fiat 128       32.4   4  78.7  66 4.08 2.200 19.47  1  1    4    1
#Honda Civic    30.4   4  75.7  52 4.93 1.615 18.52  1  1    4    2
#Toyota Corolla 33.9   4  71.1  65 4.22 1.835 19.90  1  1    4    1
#Fiat X1-9      27.3   4  79.0  66 4.08 1.935 18.90  1  1    4    1
#Porsche 914-2  26.0   4 120.3  91 4.43 2.140 16.70  0  1    5    2
#Lotus Europa   30.4   4  95.1 113 3.77 1.513 16.90  1  1    5    2
#Volvo 142E     21.4   4 121.0 109 4.11 2.780 18.60  1  1    4    2

#$`6.1`
#          mpg cyl disp  hp drat    wt  qsec vs am gear carb
#Mazda RX4     21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
#Mazda RX4 Wag 21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
#Ferrari Dino  19.7   6  145 175 3.62 2.770 15.50  0  1    5    6

#$`8.1`
#            mpg cyl disp  hp drat   wt qsec vs am gear carb
#Ford Pantera L 15.8   8  351 264 4.22 3.17 14.5  0  1    5    4
#Maserati Bora  15.0   8  301 335 3.54 3.57 14.6  0  1    5    8

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...