根据R中另一列中指定的编号报告编号列的值
我所拥有的: 资料 我想要的是:根据R中另一列中指定的编号报告编号列的值,r,dataframe,R,Dataframe,我所拥有的: 资料 我想要的是: 0_x 1_x 2_x ...10_x num_col value 0.2 0.3 0.4 . 2 0.3 0.3. 0.5 0.3 . 1 0.3 0 0.2 0.8 . 3 0.8 基本上,我想创建一个列,其值与“num_col”指示的列相对应。一个涉及dplyr的选项可以是: df %>% rowwis
0_x 1_x 2_x ...10_x num_col value
0.2 0.3 0.4 . 2 0.3
0.3. 0.5 0.3 . 1 0.3
0 0.2 0.8 . 3 0.8
基本上,我想创建一个列,其值与“num_col”指示的列相对应。一个涉及
dplyr
的选项可以是:
df %>%
rowwise() %>%
mutate(value = get(paste0(num_col-1, "_x"))) %>%
ungroup()
`0_x` `1_x` `2_x` num_col value
<dbl> <dbl> <dbl> <int> <dbl>
1 0.2 0.3 0.4 2 0.3
2 0.3 0.5 0.3 1 0.3
3 0 0.2 0.8 3 0.8
df%>%
行()
mutate(value=get(paste0(num_col-1,“\u x”))%>%
解组()
`0\u x``1\u x``2\u x`num\u列值
1 0.2 0.3 0.4 2 0.3
2 0.3 0.5 0.3 1 0.3
3 0 0.2 0.8 3 0.8
但是,它确实假设列是以某种方式命名的
样本数据:
df <- structure(list(`0_x` = c(0.2, 0.3, 0), `1_x` = c(0.3, 0.5, 0.2
), `2_x` = c(0.4, 0.3, 0.8), num_col = c(2L, 1L, 3L)), class = "data.frame", row.names = c(NA,
-3L))
df Mybase R
解决方案将是df1$value
df <- structure(list(`0_x` = c(0.2, 0.3, 0), `1_x` = c(0.3, 0.5, 0.2
), `2_x` = c(0.4, 0.3, 0.8), num_col = c(2L, 1L, 3L)), class = "data.frame", row.names = c(NA,
-3L))