TIdyverse使用列中的值进行变异以引用可能在不同行中的另一个列值

问题描述

我这样创建了一个小标题:

library(tidyverse) 
set.seed(68) 
a <- c(1,2,3,4,5) 
b <- runif(5) 
c <- c(1,1)
tib <- tibble(a,b,c)

产生这个

tib
# A tibble: 5 x 3
      a     b     c
  <dbl> <dbl> <dbl>
1     1 0.924     1
2     2 0.661     3
3     3 0.402     3
4     4 0.637     3
5     5 0.353     1

我想根据列c中给定的a值添加另一列d,即b的值。结果数据帧应如下所示:

      a     b     c     d
  <dbl> <dbl> <dbl> <dbl>
1     1 0.924     1 0.924
2     2 0.661     3 0.402
3     3 0.402     3 0.402
4     4 0.637     3 0.402
5     5 0.353     1 0.924

感谢您的光临!

解决方法

使用<!DOCTYPE html> <html lang="en" dir="ltr"> <head> <meta charset="utf-8"> <!-- Notice the leading slash. --> <link rel="stylesheet" href="/css/home.css"> <title></title> </head> <body> {{> home/home-nav}} {{{body}}} </body> </html> c的所需行建立索引:

b
tib %>% mutate(d = b[c])

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...