首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用列表的位置确定两列中每个元素的位置

如何使用列表的位置确定两列中每个元素的位置
EN

Stack Overflow用户
提问于 2021-04-19 18:27:27
回答 1查看 26关注 0票数 0

我有一个2列的数据框架

代码语言:javascript
复制
groups
                   g1                 g2
1                Soil              Feces
2                Soil               Skin
3                Soil             Tongue
4                Soil         Freshwater
6                Soil              Ocean
8                Soil               Mock
11              Feces         Freshwater
12              Feces Freshwater (creek)
14              Feces Sediment (estuary)
18               Skin Freshwater (creek)
23             Tongue Freshwater (creek)
27         Freshwater Freshwater (creek)
31 Freshwater (creek)              Ocean
33 Freshwater (creek)               Mock

因此,如果我们在g1和g2列中提取单个元素,那么我想在g1和g2列的所有元素的位置上添加两个额外的列。

代码语言:javascript
复制
l <- union(unique(g$g1), unique(g$g2))

[1] "Soil"  "Feces" "Skin" "Tongue" "Freshwater" "Freshwater (creek)" "Ocean" "Mock" "Sediment (estuary)"

因此,土壤在l中的位置对应于1,面向2,等等:

代码语言:javascript
复制
Soil               : 1
Feces              : 2
Skin               : 3
Tongue             : 4
Freshwater         : 5
Freshwater (creek) : 6
Ocean              : 7
Mock               : 8
Sediment (estuary) : 9

因此,我想在两个额外的柱中加上g1和g2中的位置基,如果土壤对应于1,粪便对应于2,那么xg1和xg2将是1和2,类似于:

代码语言:javascript
复制
   groups
                       g1                 g2       xg1         xg2
    1                Soil              Feces        1          2
    2                Soil               Skin        1          3
    3                Soil             Tongue        1          4
    4                Soil         Freshwater        1          5
    6                Soil              Ocean        1          7         
    8                Soil               Mock        1          8
    11              Feces         Freshwater        2          5
    12              Feces Freshwater (creek)        2          6
    14              Feces Sediment (estuary)        2          9
    18               Skin Freshwater (creek)        3          6
    23             Tongue Freshwater (creek)        4          6
    27         Freshwater Freshwater (creek)        5          6
    31 Freshwater (creek)              Ocean        6          7
    33 Freshwater (creek)               Mock        6          8

我试过用list (l)和命令循环,但是我做不到

太感谢了

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-04-19 19:00:09

溶液

代码语言:javascript
复制
library(tidyverse)

# creating a key
key <- structure(list(V1 = c("Soil", "Feces", "Skin", "Tongue", "Freshwater", 
"Freshwater (creek)", "Ocean", "Mock", "Sediment (estuary)"), 
    V2 = c("1", "2", "3", "4", "5", "6", "7", "8", "9")), row.names = c(NA, 
-9L), class = "data.frame")

df %>%
  mutate(
    xg1 = key$V2[match(df$g1, key$V1)],
    xg2 = key$V2[match(df$g2, key$V1)]
    )

数据

代码语言:javascript
复制
df <- structure(list(g1 = c("Soil", "Soil", "Soil", "Soil", "Soil", 
"Soil", "Feces", "Feces", "Feces", "Skin", "Tongue", "Freshwater", 
"Freshwater (creek)", "Freshwater (creek)"), g2 = c("Feces", 
"Skin", "Tongue", "Freshwater", "Ocean", "Mock", "Freshwater", 
"Freshwater (creek)", "Sediment (estuary)", "Freshwater (creek)", 
"Freshwater (creek)", "Freshwater (creek)", "Ocean", "Mock")), class = "data.frame", row.names = c(NA, 
-14L))
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67167452

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档