[英]How to use seqinr::computePI on a list of character vectors
This is what the input data looks like (representative sample).这就是输入数据的样子(代表性样本)。
sample1 MAQSVNIQDQYLNQ
sample2 MAADRAQNLQDTFLNHV
sample3 MAERSQNLQ
I am trying to use the computePI()
function from the seqinr
library to perform a calculation on the second column.我正在尝试使用seqinr
库中的computePI()
函数对第二列执行计算。 To do this, I need to convert the character strings in the second column to a vector of single characters, and use that as input for computePI()
.为此,我需要将第二列中的字符串转换为单个字符的向量,并将其用作computePI()
输入。 To do this, I tried using strsplit()
.为此,我尝试使用strsplit()
。 It works on an individual string:它适用于单个字符串:
library(seqinr)
str <- c("MAQSVNIQDQYLNQ")
unlist(strsplit(str, split = ""))
#R> [1] "M" "A" "Q" "S" "V" "N" "I" "Q" "D" "Q" "Y" "L" "N" "Q"
Followed by:其次是:
computePI(unlist(strsplit(str, split = "")))
#R> [1] 3.799404
After saving the input data read using read.csv
to a variable:将使用read.csv
读取的输入数据read.csv
到变量后:
data <- read.csv("filename", header = FALSE, sep = "\t")
I tried the following:我尝试了以下方法:
computePI(unlist(strsplit(data$V2, split = "")))
But got an error telling me that the input isn't a character string.但是有一个错误告诉我输入不是字符串。 How can I fix this?我怎样才能解决这个问题?
您可以使用sapply
:
sapply(strsplit(data$V2, split = ""), seqinr::computePI)
You can use Vectorize
:您可以使用Vectorize
:
library(seqinr)
VcomputePI <- Vectorize(computePI)
VcomputePI(strsplit(dat$V2, split = ""))
#R> [1] 3.799404 5.194135 5.753213
Or sapply
/ lapply
/ vapply
like as Ronak Shah shows.或者像Ronak Shah展示的那样sapply
/ lapply
/ vapply
。 vapply
is likely the fastest: vapply
可能是最快的:
vapply(strsplit(dat$V2, split = ""), computePI, 0.)
#R> [1] 3.799404 5.194135 5.753213
dat <- data.frame(
V1 = paste0("sample", 1:3),
V2 = c("MAQSVNIQDQYLNQ", "MAADRAQNLQDTFLNHV", "MAERSQNLQ"),
stringsAsFactors = FALSE)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.