我有以下df:
Marker CHR val Position
1 1 2.10 1
2 1 2.11 10
3 1 3.33 20
4 1 3.55 30
5 1 2.06 40
6 2 2.03 1
7 2 3.04 10
8 2 3.10 20
9 2 3.05 30
10 2 2.90 40
如何使用基于列位置的窗口大小基于val值(例如val> 3)分配组。
预期输出是什么?也许像这样吗?
library(tidyverse)
test <- read_csv('sample.csv')
test %>% mutate(group = case_when(val >= 20 & val <= 49 ~ 'Group 1',
val >= 50 & val <= 69 ~ 'Group 2',
val >= 70 & val <= 100 ~ 'Group 3')) %>%
group_by(group)
# A tibble: 9 x 5
# Groups: group [3]
Marker CHR val Position group
<dbl> <dbl> <dbl> <dbl> <chr>
1 1 1 20 5 Group 1
2 2 1 30 6 Group 1
3 3 2 40 7 Group 1
4 4 2 50 8 Group 2
5 5 3 60 9 Group 2
6 6 4 70 10 Group 3
7 7 2 80 11 Group 3
8 8 1 90 12 Group 3
9 9 1 100 13 Group 3
也许这也有帮助
library(dplyr)
df1 %>%
mutate(group = cut(val, breaks = c(20, 50, 70, 100),
labels = c('Group 1', 'Group 2', 'Group 3')))