简体   繁体   中英

Recoding of data

I have looked through previous questions on recoding and cannot find answers to my question hence why i hope that someone can help me with my question.

The data below are expressed in degrees and show the different classes that aspect has been grouped into. I am trying to recode this variable so each "portion of the circle" is coded from 1-9. I can convert it if the starting point is from a single number but as in this case there is a range (eg0-22.5) involved i am a little puzzled

Aspect
0,22.5 (1)
22.5,67.5 (2)
67.5,112 (3)
112,158 (4)
158,202 (5)
202,248 (6)
248,292 (7)
292,338 (8)
338,360 (9)

Obviously as this is data based on aspect it is a continous scale from 0-360 (circle). Hoping someone can help

Just to add clarity to @BenBolker's comment above:

df = data.frame("original" = 1:360)
df$category = cut(df$original,
                  breaks = c(0, 22.5, 67.5, 112, 158, 202, 248, 292, 338, 360),
                  labels = 1:9)

summary(df$category)

Please note that your class ranges look like they're slightly out by half a degree sometimes (maybe a rounding error). I would have thought that every segment should be 45 degrees apart from the top 2.

Eg

0,22.5 (1)
22.5,67.5 (2)
67.5,112.5 (3)
112.5,157.5 (4)
157.5,202.5 (5)
202.5,247.5 (6)
247.5,292.5 (7)
292.5,337.5 (8)
337.5,360 (9)

If you dont mind these new values and you are trying to obtain the class from the number of degrees, then you could do this.

  1. Divide by 45
  2. Add 1
  3. Round to nearest whole number

Eg 230 degrees

230 / 45 = 5.1111
5.1111 + 1 = 6.1111
round(6.111) = Class 6

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM