Home > Blockchain >  Change factor column labels based on vector in R
Change factor column labels based on vector in R

Time:10-08

I wasn't entirely sure how to phrase the title. This is what I need to be done:

I have a vector which contains the correct order of my factor labels, the first few elements are as follows:

   [1] 2227DD 2228BD 2228CA 2228CB 2228CC 2228CD 2228DA 2228DB 2228DC 2228DD 2229AA 2229AB 2229AC 2229AD 2229BA 2229BB 2229BC
  [18] 2229BD 2229CA 2229CB 2229CC 2229CD 2229DA 2229DB 2229DC 2229DD 2230AA 2230AC 2230AD 2230BC 2230BD 2230CA 2230CB 2230CC
  [35] 2230CD 2230DA 2230DB 2230DC 2230DD 2231AC 2231AD 2231CA 2231CB 2231CC 2231CD 2326DB 2326DD 2327AC 2327AD 2327BA 2327BB
  [52] 2327BC 2327BD 2327CA 2327CB 2327CC 2327CD 2327DA 2327DB 2327DC 2327DD 2328AA 2328AB 2328AC 2328AD 2328BA 2328BB 2328BC

I then have a dataframe that looks like this:

> head(Visit.data_allyears[[1]],10)
   SiteName year PAdata Longitude Latitude    totalspp  totalhours       lhours temperature    rainfall        NDVI   SA_elevati
1    2229AB 2007      0    29.375  -22.125  0.27388999  0.04145321  0.359057436   0.7571729  0.34862768  0.25624133 -0.401474904
2    2230CA 2007      0    30.125  -22.625 -0.46728113 -0.43741429 -0.460164072   0.8803066 -0.76683748 -0.15804871 -0.243873619
3    2230DA 2007      0    30.625  -22.625 -0.79669052  0.28088696  0.670510998   1.0815264 -0.86448501 -0.68218838 -0.009113371
4    2230DB 2007      0    30.875  -22.625 -1.99079956 -0.43741429 -0.460164072   1.3638363 -0.92470284 -0.86108636 -0.475350506
5    2231AC 2007      0    31.125  -22.375  2.82681276 -0.43741429 -0.460164072   0.8652892  1.39814838  1.64976237 -0.867712039
6    2231AC 2007      0    31.125  -22.375  1.30329433  0.04145321  0.359057436   0.8652892  1.39814838  1.64976237 -0.867712039
7    2231AC 2007      0    31.125  -22.375  0.80918025 -0.67684804 -1.041410982   0.8652892  1.39814838  1.64976237 -0.867712039
8    2231AC 2007      0    31.125  -22.375  1.75623224  0.52032071  0.940304346   0.8652892  1.39814838  1.64976237 -0.867712039
9    2231AC 2007      0    31.125  -22.375  2.66210806  1.95692322  2.070979417   0.8652892  1.39814838  1.64976237 -0.867712039
10   2231AC 2007      0    31.125  -22.375  1.13858964 -0.67684804 -1.041410982   2.2918537  0.51288305 -0.40285646 -0.867712039

The data-frame does not contain all the site ids that are in the vector, but some are repeated. I want the vector elments to correspond to 1 to 2026:

I want 2227DD to be 1, 2228BD to be 2 and so forth and then have the dataframe be:

   SiteName year PAdata Longitude Latitude    totalspp  totalhours       lhours temperature    rainfall        NDVI   SA_elevati
1    12 2007      0    29.375  -22.125  0.27388999  0.04145321  0.359057436   0.7571729  0.34862768  0.25624133 -0.401474904
2    32 2007      0    30.125  -22.625 -0.46728113 -0.43741429 -0.460164072   0.8803066 -0.76683748 -0.15804871 -0.243873619
3    36 2007      0    30.625  -22.625 -0.79669052  0.28088696  0.670510998   1.0815264 -0.86448501 -0.68218838 -0.009113371
4    37 2007      0    30.875  -22.625 -1.99079956 -0.43741429 -0.460164072   1.3638363 -0.92470284 -0.86108636 -0.475350506
5    40 2007      0    31.125  -22.375  2.82681276 -0.43741429 -0.460164072   0.8652892  1.39814838  1.64976237 -0.867712039
6    40 2007      0    31.125  -22.375  1.30329433  0.04145321  0.359057436   0.8652892  1.39814838  1.64976237 -0.867712039
7    40 2007      0    31.125  -22.375  0.80918025 -0.67684804 -1.041410982   0.8652892  1.39814838  1.64976237 -0.867712039
8    40 2007      0    31.125  -22.375  1.75623224  0.52032071  0.940304346   0.8652892  1.39814838  1.64976237 -0.867712039
9    40 2007      0    31.125  -22.375  2.66210806  1.95692322  2.070979417   0.8652892  1.39814838  1.64976237 -0.867712039
10   40 2007      0    31.125  -22.375  1.13858964 -0.67684804 -1.041410982   2.2918537  0.51288305 -0.40285646 -0.867712039

CodePudding user response:

You may use match, if the vector is called vec.

Visit.data_allyears[[1]]$SiteName <- match(Visit.data_allyears[[1]]$SiteName, vec)
  • Related