- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试创建一个能够将来自 8 个字符的字符串(例如 AAARLAA)的字母转换为 20 位代码(10000000...)的函数,然后连接结果,以便从我获得的 8 个字符的字符串中获得结果一个 160 个整数向量(其中每 20 个数字对应一个字母)。
这是我的脚本
octamer_encoding <- function(octamero){
resultado_palabras <- vector("integer", length(octamero))
A = c(1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)
R = c(0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)
N = c(0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)
D = c(0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)
C = c(0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)
E = c(0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0)
Q = c(0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0)
G = c(0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0)
H = c(0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0)
I = c(0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0)
L = c(0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0)
K = c(0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0)
M = c(0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0)
F = c(0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0)
P = c(0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0)
S = c(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0)
T = c(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0)
W = c(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0)
Y = c(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0)
V = c(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1)
numeros <- rbind(A,R,N,D,C,E,Q,G,H,I,L,K,M,F,P,S,T,W,Y,V)
letras <- c('A','R','N','D','C','E','Q','G','H','I','L','K','M','F','P','S','T','W','Y','V')
n = length(octamero)
h = length(letras)
for (j in 1:n){
for (k in 1:h){
if ((substring(octamero, first = j, last = j)) == (letras[k])){
resultado_palabras <- c(resultado_palabras, numeros[k,])
}
}
}
return(resultado_palabras)
}
出于某种原因,我获得的结果只是一个 20 数字向量,对应于字符串的第一个字母,这意味着循环无法连接不同的结果,但我不明白我在做什么错了。
最佳答案
您需要进行一些更改。您应该在下面看到它们:
octamer_encoding <- function(octamero){
resultado_palabras <- c() ######### EDIT 1: YOU NEED TO CREATE AN EMPTY VECTOR FIRST
A = c(1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)
R = c(0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)
N = c(0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)
D = c(0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)
C = c(0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)
E = c(0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0)
Q = c(0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0)
G = c(0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0)
H = c(0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0)
I = c(0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0)
L = c(0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0)
K = c(0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0)
M = c(0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0)
F = c(0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0)
P = c(0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0)
S = c(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0)
T = c(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0)
W = c(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0)
Y = c(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0)
V = c(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1)
numeros <- rbind(A,R,N,D,C,E,Q,G,H,I,L,K,M,F,P,S,T,W,Y,V)
letras <- c('A','R','N','D','C','E','Q','G','H','I','L','K','M','F','P','S','T','W','Y','V')
n = nchar(octamero) ############ EDIT 2: nchar instead of length.
h = length(letras)
for (j in 1:n){
for (k in 1:h){
if ((substring(octamero, first = j, last = j)) == (letras[k])){
resultado_palabras <- c(resultado_palabras, numeros[k,])
}
}
}
return(unname(resultado_palabras))
}
另外,我冒昧地简化了您的代码来帮助您:
octamer_encoding2 <- function(octamero){
letras <- c('A','R','N','D','C','E','Q','G','H','I','L','K','M','F','P','S','T','W','Y','V')
numeros <- diag(1, length(letras))
colnames(numeros) <- letras
c(numeros[,strsplit(octamero, "")[[1]]])
}
octamer_encoding("AAARLAA")
#> [1] 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0
#> [46] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
#> [91] 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0
#> [136] 0 0 0 0 0
它的功能完全相同,但更紧凑。
而且速度更快:
microbenchmark::microbenchmark(octamer_encoding("AAARLAA"),
octamer_encoding2("AAARLAA"))
#> Unit: microseconds
#> expr min lq mean median uq max neval
#> octamer_encoding("AAARLAA") 604.3 632.95 735.284 673.2 760.70 1278.1 100
#> octamer_encoding2("AAARLAA") 17.5 19.75 31.425 24.3 36.95 132.8 100
如果您对 letras
的不同(且更紧凑)编码感兴趣,您可以仅使用 5 个数字来识别 20 个不同的字母。
您可以按如下方式定义numeros
:
library(binaryLogic)
l <- length(letras)
numeros <- +simplify2array(as.binary(seq_len(l), n = ceiling(logb(l,2))))
colnames(numeros) <- letras
numeros
#> A R N D C E Q G H I L K M F P S T W Y V
#> [1,] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1
#> [2,] 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 0 0 0 0 0
#> [3,] 0 0 0 1 1 1 1 0 0 0 0 1 1 1 1 0 0 0 0 1
#> [4,] 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0
#> [5,] 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0
关于r - 我的循环没有进行应有的串联,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64600281/
我正在尝试编写一行简单的代码,用于从各种服务器的 C 盘中删除各种用户文件。如何与 PowerShell 连接以获取到服务器的路径? 例如,这就是我想要做的,但 PowerShell 没有将 + 符号
我一直在尝试合并(连接)两个具有相同高度和宽度的 mp4 视频,但出现了一些错误。 代码是: FFmpeg ffmpeg = FFmpeg.getInstance(EditVideoActivity.
我正在尝试在 PostgreSQL 中进行简单的串联,但它不断抛出一条错误消息。我不明白我在这里做错了什么。 select concat('abcde', 'fgh'); No function ma
我不知道如何从一个函数中获取两个变量,这是我的菜鸟尝试,这可能吗?如果可以,它是如何完成的? #include float seriesParallel (float r1, float r2);/
我有以下代码: char stringHour[50], stringMinute[50], stringSecond[50]; // lots of code... itoa(hour, strin
我是套接字编程的新手,我正在尝试使用 TCP 实现客户端服务器。客户端在 Windows 中用 Java 编写,服务器在 Tandem/Hp-NonStop 中用 C 编写。我能够连接并向服务器发送请
到目前为止,我有以下内容。 DECLARE @Table Table (ID int, Value1 varchar(50), Value2 varchar(50), Value3 varchar(5
我是 Elasticsearch 的新手,想解决以下问题的用例 我有一些格式如下的文件 { "member_id":"number" "e_id":"number", "c_id":"numbe
我有这个功能来懒惰地观看日志文件... follow :: Handle -> IO [String] follow h = unsafeInterleaveIO $ do catch (do l
这个问题已经有答案了: List of lists changes reflected across sublists unexpectedly (17 个回答) 已关闭 3 年前。 我想创建一个没有
我们有一个 XML 文件,我们正试图找出一种动态使用的方法。 基本知识是这样的: 100.00 110.00 120.00 130.00 对于我们拥有的每个零件,我们希望在其上使用 xslt 以
我有一个很奇怪的问题。 下面的所有代码都工作正常: payload.modules.forEach(function(mod) { var modules_path = '..
如何使用 C 预处理自动生成以下代码模式(访问器)? // Immutable accessor. const auto& member1 () const { return _member1;
我有时间值来自数据库:00:30:00 这是我的代码: var gdtesttime = ; // 00:30:00 console.log(gdtesttime); var a = gdt
数据库:MySQL 我有两张表,一张用于用户分配的角色,一张包含角色信息。我的问题是分配的角色存储在单个字段中,用逗号分隔。我需要构建一个按名称而非 ID 列出角色的报告,但仍位于由列分隔的单个字段中
我在 ffmpeg 中使用以下命令,它会在视频开头添加 1 秒的黑帧。但是,我在输出视频中丢失了原始视频中的音频。我如何调整命令以确保原始音频与最终输出保持一致,或者更好的是,开头有 1 秒的“空白”
表 A 有 X 列,它是由表 B 中的 Y 列和 Z 列(均为 float )串联而成的 int。我想以类似于此的方式连接表 A 和 B: select * from tableA a inner j
pd.Series([1,1,1,1, "something", 1]).astype(float).cumsum().fillna(None) 我想在上面的系列中用 None 填充 NaN,但它告诉
在创建显示 OpenFileDialog/SaveFileDialog 的服务时,我正在考虑创建 LINQ 查询/清除 C# 代码到 Concatinate()/Join() 过滤表达式。 根据这个调
我正在尝试从数组中提取值并将所有值连接到一个字符串中,如下所示 var ingredient: String for tag in tags { if let text = tag.titleL
我是一名优秀的程序员,十分优秀!