我读了所有'字符串到变量名'的帖子,但没有一个涵盖我的特定问题。我有一个使用seqinr包'read.fasta'制作的载体列表(DNA序列数据)。我有变量的数据框及其位置,我想将数据框中指定位置的列表向量元素转换为其替代值。在个人基础上,这可以使用
完成list$name[number] <- alternate.character
# I tried
for (i in 1:length(df$CHROM))
if (is.na(df$Call[i])) {next} else {get(paste("test$",df$CHROM[i],"[",df$POS[i],"]",sep="")) <- df$Call[i]}
# example data
test <- list("One" = c("a","t","a","g","c"),
"Two" = c("g","a","t","t","a","c","a"))
df <- data.frame("CHROM"=c(rep("One",2),rep("Two",3)),
"POS" = c(2,4,1,3,6),
"REF" = c("t","g","g","t","c"),
"ALT" = c("a","a","t","g","t"),
"Call" = c("T","A","G",NA,"T"))
但是'get'会从列表中返回vector元素,并且不允许我将其指定为父列表中的变体。
我想要的是来自
的列表$One
[1] "a" "t" "a" "g" "c"
$Two
[1] "g" "a" "t" "t" "a" "c" "a"
到
$One
[1] "a" "T" "a" "A" "c"
$Two
[1] "G" "a" "t" "t" "a" "T" "a"
对于测试数据,这不是问题,因为您可以单独执行此操作,但实际数据超过10,000个序列和100,000多个变体。如果您可以对其进行矢量化,则我没有足够的经验来嵌套应用函数,以使其同时处理列表和数据框中的信息。
sessionInfo()
R version 3.0.2 (2013-09-25)
Platform: x86_64-pc-linux-gnu (64-bit)
locale:
[1] LC_CTYPE=en_GB.UTF-8 LC_NUMERIC=C
[3] LC_TIME=en_GB.UTF-8 LC_COLLATE=en_GB.UTF-8
[5] LC_MONETARY=en_GB.UTF-8 LC_MESSAGES=en_GB.UTF-8
[7] LC_PAPER=en_GB.UTF-8 LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_GB.UTF-8 LC_IDENTIFICATION=C
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] seqinr_3.0-7
loaded via a namespace (and not attached):
[1] tools_3.0.2
答案 0 :(得分:1)
您可以使用sapply
执行此任务:
res <- sapply(names(test), function(x) {
tmp <- df[df$CHROM == x & !is.na(df$Call), ]
replace(test[[x]], tmp$POS, as.character(tmp$Call))
})
res
# $One
# [1] "a" "T" "a" "A" "c"
#
# $Two
# [1] "G" "a" "t" "t" "a" "T" "a"