gpt4 book ai didi

r - 根据 R 中的其他 df 值更改列值

转载 作者:行者123 更新时间:2023-12-04 08:47:33 24 4
gpt4 key购买 nike

你好我有一个 df1 :

scaf_name       coordinates value    
JZSA01000001.1 1 2
JZSA01000001.1 2 2
JZSA01000001.1 3 2
JZSA01000001.1 4 2
JZSA01000001.1 5 2
JZSA01000001.1 6 2
JZSA01000001.1 7 2
JZSA01000001.1 8 2
JZSA01000001.1 9 2
JZSA01000001.1 10 2
JZSA01000001.1 11 5
JZSA01000001.1 12 5
JZSA01000001.1 13 5
JZSA01000001.1 14 5
JZSA01000001.1 15 5
JZSA01000001.1 16 5
JZSA01000001.1 17 5
JZSA01000001.1 18 6
JZSA01000002.1 1 2
JZSA01000002.1 2 2
JZSA01000002.1 3 2
JZSA01000002.1 4 2
JZSA01000002.1 5 2
JZSA01000002.1 6 2
JZSA01000003.1 1 5
JZSA01000003.1 2 5
JZSA01000003.1 3 6
JZSA01000003.1 4 6
JZSA01000003.1 5 6
JZSA01000003.1 6 6
JZSA01000003.1 7 6
JZSA01000003.1 8 6
JZSA01000003.1 9 6
和另一个 df_interval
scaffold          start     end
JZSA01000001.1_0 1 14
JZSA01000001.1_1 15 18
JZSA01000002.1 1 12
JZSA01000003.1_0 1 3
JZSA01000003.1_1 4 6
JZSA01000003.1_2 7 9
我想更改 df1$scaf_name根据 df1$scaf_name$startdf1$scaf_name$end所以例如
df1$scaf_name包含 df_interval$scaffold哪里 df1$coordinates操作系统之间 1-14将被命名为 JZSA01000001.1_0在这里我应该得到作为输出
scaf_name       coordinates value    
JZSA01000001.1_0 1 2
JZSA01000001.1_0 2 2
JZSA01000001.1_0 3 2
JZSA01000001.1_0 4 2
JZSA01000001.1_0 5 2
JZSA01000001.1_0 6 2
JZSA01000001.1_0 7 2
JZSA01000001.1_0 8 2
JZSA01000001.1_0 9 2
JZSA01000001.1_0 10 2
JZSA01000001.1_0 11 5
JZSA01000001.1_0 12 5
JZSA01000001.1_0 13 5
JZSA01000001.1_0 14 5
JZSA01000001.1_1 15 5
JZSA01000001.1_1 16 5
JZSA01000001.1_1 17 5
JZSA01000001.1_1 18 6
JZSA01000002.1 1 2
JZSA01000002.1 2 2
JZSA01000002.1 3 2
JZSA01000002.1 4 2
JZSA01000002.1 5 2
JZSA01000002.1 6 2
JZSA01000003.1_0 1 5
JZSA01000003.1_0 2 5
JZSA01000003.1_0 3 6
JZSA01000003.1_1 4 6
JZSA01000003.1_1 5 6
JZSA01000003.1_1 6 6
JZSA01000003.1_2 7 6
JZSA01000003.1_2 8 6
JZSA01000003.1_2 9 6
df1 文件非常大,如果有人想成为最快的,那就太棒了。
谢谢
数据
df1
structure(list(scaf_name = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L,
2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("JZSA01000001.1",
"JZSA01000002.1", "JZSA01000003.1"), class = "factor"), coor = c(1L,
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L,
16L, 17L, 18L, 1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L,
7L, 8L, 9L), dinates.value = c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 2L, 2L, 2L, 2L, 2L, 2L,
5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L)), class = "data.frame", row.names = c(NA,
-33L))
df_interval
structure(list(scaffold = structure(1:6, .Label = c("JZSA01000001.1_0", 
"JZSA01000001.1_1", "JZSA01000002.1", "JZSA01000003.1_0", "JZSA01000003.1_1",
"JZSA01000003.1_2"), class = "factor"), start = c(1L, 15L, 1L,
1L, 4L, 7L), end = c(14L, 18L, 12L, 3L, 6L, 9L)), class = "data.frame", row.names = c(NA,
-6L))

最佳答案

我们可以使用来自 data.table 的非对等连接

library(data.table)
setDT(df1)[df_interval, scaf_name := scaffold,
on = .(coordinates >= start, coordinates <= end)]

关于r - 根据 R 中的其他 df 值更改列值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64235332/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com