好吧,我知道这可能是一个简单的答案。我只是在这一点上盯着代码看得太久了。无论如何,我已经访问了Grouping data into ranges in R,R Data Grouping,grouping character variables in R,R - Grouping of Data,grouping data in R,和其他多个。我提前道歉,这是一个重复,但我还没有复制。
我有来自20多个病毒家族的数据。我已经将其编码为二进制,因此每行都有一个唯一的标识符,并且在每个病毒家族列中有一个0或1。我正在尝试将研究中心的数据编码为数字组。
数据(样本量)
date Site geometry
<chr> <chr> <S3: sfc_POINT>
8.20.13 NDUFR <S3: sfc_POINT>
8.27.13 UMNP-HQ <S3: sfc_POINT>
8.28.13 UMNP-campsite3 <S3: sfc_POINT>
8.28.13 UMNP-campsite3 <S3: sfc_POINT>
8.28.13 UMNP-hondohondoa <S3: sfc_POINT>
8.28.13 UMNP-hondohondob <S3: sfc_POINT>
8.29.13 UMNP-njokamoni <S3: sfc_POINT>
8.29.13 UMNP-mangabey <S3: sfc_POINT>
8.30.13 UMNP-hondohondoc <S3: sfc_POINT>
8.30.13 UMNP-hondohondod <S3: sfc_POINT>
8.30.13 UMNP-hondohondoe <S3: sfc_POINT>
8.31.13 UMNP-HQ <S3: sfc_POINT>
8.31.13 MamaGoti <S3: sfc_POINT>
9.1.13 UMNP-Sanje1 <S3: sfc_POINT>
9.1.13 UMNP-Sanje2 <S3: sfc_POINT>
9.1.13 UMNP-Sanje3 <S3: sfc_POINT>
9.2.13 Magombera1 <S3: sfc_POINT>
9.2.13 Magombera2 <S3: sfc_POINT>
9.3.13 Sonjo <S3: sfc_POINT>
9.3.13 SonjoRoad <S3: sfc_POINT>
我想将NDUFR编码为1,将UMNP编码为2,将UMNP-营地3编码为3,然后将UMNP-hondohondoa、UMNP-hondohondob、UMNP-hondohondoc、UMNP-hondohondod和UMNP-hondohondoe分组为4 MamaGoti到5 UMNP-Sanje 1、UMNP-Sanje 2和UMNP-Sanje 3到6 Magombera 1和Magombera 2到7 Sonjo和SonjoRoad到8
我知道这可能相对容易,但我已经尝试了许多dplyr和base r的建议。
任何帮助都将不胜感激。
数据输出
structure(list(date = c("8.20.13", "8.27.13", "8.28.13", "8.28.13",
"8.28.13", "8.28.13", "8.29.13", "8.29.13", "8.30.13", "8.30.13",
"8.30.13", "8.31.13", "8.31.13", "9.1.13", "9.1.13", "9.1.13",
"9.2.13", "9.2.13", "9.3.13", "9.3.13"), forestsite = c("NDUFR",
"UMNP-HQ", "UMNP-campsite3", "UMNP-campsite3", "UMNP-hondohondoa",
"UMNP-hondohondob", "UMNP-njokamoni", "UMNP-mangabey", "UMNP-hondohondoc",
"UMNP-hondohondod", "UMNP-hondohondoe", "UMNP-HQ", "MamaGoti",
"UMNP-Sanje1", "UMNP-Sanje2", "UMNP-Sanje3", "Magombera1", "Magombera2",
"Sonjo", "SonjoRoad"), geometry = structure(list(structure(c(35.908246,
-8.0475655), class = c("XY", "POINT", "sfg")), structure(c(36.883988,
-7.844929), class = c("XY", "POINT", "sfg")), structure(c(36.884545,
-7.849439), class = c("XY", "POINT", "sfg")), structure(c(36.884545,
-7.849439), class = c("XY", "POINT", "sfg")), structure(c(36.887065,
-7.833501), class = c("XY", "POINT", "sfg")), structure(c(36.891369,
-7.832091), class = c("XY", "POINT", "sfg")), structure(c(36.878388,
-7.82738), class = c("XY", "POINT", "sfg")), structure(c(36.87753,
-7.8232), class = c("XY", "POINT", "sfg")), structure(c(36.89072,
-7.829972), class = c("XY", "POINT", "sfg")), structure(c(36.890019,
-7.827783), class = c("XY", "POINT", "sfg")), structure(c(36.887414,
-7.825414), class = c("XY", "POINT", "sfg")), structure(c(36.883988,
-7.844929), class = c("XY", "POINT", "sfg")), structure(c(36.886217,
-7.844622), class = c("XY", "POINT", "sfg")), structure(c(36.904182,
-7.783986), class = c("XY", "POINT", "sfg")), structure(c(36.903943,
-7.783), class = c("XY", "POINT", "sfg")), structure(c(36.902821,
-7.77507), class = c("XY", "POINT", "sfg")), structure(c(36.980875,
-7.832182), class = c("XY", "POINT", "sfg")), structure(c(36.960576,
-7.815916), class = c("XY", "POINT", "sfg")), structure(c(36.896019,
-7.808054), class = c("XY", "POINT", "sfg")), structure(c(36.895821,
-7.81365), class = c("XY", "POINT", "sfg"))), class = c("sfc_POINT",
"sfc"), precision = 0, bbox = structure(c(xmin = 35.908246, ymin = -8.0475655,
xmax = 36.980875, ymax = -7.77507), class = "bbox"), crs = structure(list(
input = "EPSG:4326", wkt = "GEOGCRS[\"WGS 84\",\n ENSEMBLE[\"World Geodetic System 1984 ensemble\",\n MEMBER[\"World Geodetic System 1984 (Transit)\"],\n MEMBER[\"World Geodetic System 1984 (G730)\"],\n MEMBER[\"World Geodetic System 1984 (G873)\"],\n MEMBER[\"World Geodetic System 1984 (G1150)\"],\n MEMBER[\"World Geodetic System 1984 (G1674)\"],\n MEMBER[\"World Geodetic System 1984 (G1762)\"],\n MEMBER[\"World Geodetic System 1984 (G2139)\"],\n ELLIPSOID[\"WGS 84\",6378137,298.257223563,\n LENGTHUNIT[\"metre\",1]],\n ENSEMBLEACCURACY[2.0]],\n PRIMEM[\"Greenwich\",0,\n ANGLEUNIT[\"degree\",0.0174532925199433]],\n CS[ellipsoidal,2],\n AXIS[\"geodetic latitude (Lat)\",north,\n ORDER[1],\n ANGLEUNIT[\"degree\",0.0174532925199433]],\n AXIS[\"geodetic longitude (Lon)\",east,\n ORDER[2],\n ANGLEUNIT[\"degree\",0.0174532925199433]],\n USAGE[\n SCOPE[\"Horizontal component of 3D system.\"],\n AREA[\"World.\"],\n BBOX[-90,-180,90,180]],\n ID[\"EPSG\",4326]]"), class = "crs"), n_empty = 0L)), row.names = c(NA,
-20L), sf_column = "geometry", agr = structure(c(date = NA_integer_,
forestsite = NA_integer_), .Label = c("constant", "aggregate",
"identity"), class = "factor"), class = c("sf", "tbl_df", "tbl",
"data.frame"))
2条答案
按热度按时间dwthyt8l1#
你可以使用
case_when
来实现这个目的,注意在case_when
中条件必须从最具体到最不具体。2exbekwf2#
case_match()
可以是稍短的替代: