我正在使用文本格式读取数据文件readLines.第一个"列"是我不需要的复杂文本.下一列包含我需要的数据.第一个"列"和数据用冒号(:)分隔.我希望在第一个冒号处拆分每一行并删除生成的文本字符串,仅保留数据.
下面是一个示例数据文件.一个潜在的复杂因素是一行数据包含多个冒号.这条线可能在某些时候成为我的标题.所以,我可能不应该在每个结肠处分开,只是在第一个结肠处.
my.data <- "first string of text..: aa : bb : cc
next string ........ : 2 0 2
third string......1990: 7 6 5
last string : 4 2 3"
my.data2 <- readLines(textConnection(my.data))
my.data2
Run Code Online (Sandbox Code Playgroud)
我试过这里提供的代码:
和这里:
上面第一个链接的代码似乎只在第一行的第一个冒号处分开.第二个链接的代码可能会做我想要的,但对我来说太复杂到目前为止无法成功修改它.
这是我希望获得的数据,此时我可以使用一个非常简单的gsub语句简单地用空格替换第一行中剩余的冒号:
aa : bb : cc
2 0 2
7 6 5
4 2 3
Run Code Online (Sandbox Code Playgroud)
很抱歉,如果这是我未找到的帖子的副本,感谢您提供任何建议或帮助.
我有一个方法,我想用它来替换字符串中的字符:
def complexity_level_two
replacements = {
'i' => 'eye', 'e' => 'eei',
'a' => 'aya', 'o' => 'oha'}
word = "Cocoa!55"
word_arr = word.split('')
results = []
word_arr.each { |char|
if replacements[char] != nil
results.push(char.to_s.gsub!(replacements[char]))
else
results.push(char)
end
}
end
Run Code Online (Sandbox Code Playgroud)
我想要的字符串输出应该是: Cohacohaa!55
但是,当我运行此方法时,它不会替换字符,只输出字符串:
C
o
c
o
a
!
5
5
Run Code Online (Sandbox Code Playgroud)
我在做什么错误,这个方法不会替换字符串中的正确字符以匹配中的字符,hash以及如何解决此问题以获得所需的输出?
假设我有一个像这样的数据框,带有字符串向量var2
var1 var2
1 abcdefghi
2 abcdefghijklmnop
3 abc
4 abcdefghijklmnopqrst
Run Code Online (Sandbox Code Playgroud)
将每n个字符的var2拆分为新列的最有效方法是什么,直到每个字符串结束为止,
例如,如果每4个字符,输出将如下所示:
var1 var2 new_var1 new_var2 new_var3 new_var4 new_var5
1 abcdefghi abcd efgh i
2 abcdefghijklmnop abcd efgh ijkl mnop
3 abc abc
4 abcdefghijklmnopqrst abcd efgh ijkl mnop qrst
Run Code Online (Sandbox Code Playgroud)
stringr包?使用"str_split_fixed"
或使用正则表达式:
gsub("(.{4})", "\\1 ", "abcdefghi")
Run Code Online (Sandbox Code Playgroud)
根据var2的长度创建转到new_var_n的新列的容量,例如可以是10000个字符.
提前致谢...
我在 ruby 中的正则表达式方面遇到了一些麻烦,或者找到了从字符串中删除斜杠的方法。这是我的字符串的样子:
string = "word \/ word"
Run Code Online (Sandbox Code Playgroud)
我正在尝试删除反斜杠和斜杠;我想要这个结果:
string = "word word"
Run Code Online (Sandbox Code Playgroud)
我想我错过了一些带有转义字符的东西,或者谁知道是什么!
我已经尝试过这个:
string.gsub(/\//, "")
Run Code Online (Sandbox Code Playgroud)
这将删除反斜杠,但保留斜杠。我已经尝试过在各处以及甚至没有意义的地方使用转义字符进行变体!
我对正则表达式很糟糕,并且对一般的字符串工作感到非常沮丧,我只是不知所措。我确信这是显而易见的,但我错过了什么?
最后一个例子用来工作,现在却没有.函数内部没有字符串插值?也许它与变量范围有关?有什么建议?
library(gsubfn)
#R.Version() # I'm using 2.15.0, just upgraded from 2.13.something.
### dumb example of a function
g <- function() {for (a in 1:2) { print(paste('is a', a)) }}
g()
### same thing, outside a function, using string interpolation
for (a in 1:2) { fn$print('is a $a') }
rm(a)
### and now string interpolation inside a function
h <- function() {for (a in 1:2) { fn$print('is a $a') }}
h()
Run Code Online (Sandbox Code Playgroud)
最后一个例子告诉我
Error in eval(expr, envir, enclos) : object 'a' not …Run Code Online (Sandbox Code Playgroud) 我有一些mathjax格式的HTML文本:
text = "an inline \\( f(x) = \frac{a}{b} \\) equation, a display equation \\[ F = m a \\] \n and another inline \\(y = x\\)"
Run Code Online (Sandbox Code Playgroud)
(注意:方程式由单斜线分隔,例如\(,不是\\(,额外\的只是逃避红宝石文本的第一个).
我想得到替代它的输出,例如latex.codecogs创建的图像,例如
desired_output = "an inline <img src="http://latex.codecogs.com/png.latex?f(x) = \frac{a}{b}\inline"/> equation, a display equation <img src="http://latex.codecogs.com/png.latex?F = m a"/> \n and another inline <img src="http://latex.codecogs.com/png.latex?y = x\inline"/> "
Run Code Online (Sandbox Code Playgroud)
使用Ruby.我尝试:
desired = text.gsub("(\\[)(.*?)(\\])", "<img src=\"http://latex.codecogs.com/png.latex?\2\" />")
desired = desired.gsub("(\\()(.*?)(\\))", "<img src=\"http://latex.codecogs.com/png.latex?\2\\inline\")
desired
Run Code Online (Sandbox Code Playgroud)
但这不成功,仅返回原始输入.我错过了什么?如何正确构造此查询?
我想重新编码一些标识符,从大写到小写.
我不确定这里的问题是什么.
n = c('AFD.434', 'BSD.23', 'F234.FF')
gsub(pattern = '[[:upper:]]', replacement = '[[:lower:]]', n)
[1] "[[:lower:]][[:lower:]][[:lower:]].434" "[[:lower:]][[:lower:]][[:lower:]].23" "[[:lower:]]234.[[:lower:]][[:lower:]]"
Run Code Online (Sandbox Code Playgroud)
有什么建议?
我在R中使用gsub将文本添加到字符串的中间.它工作得很好,但由于某种原因,当位置太长时,它会抛出错误.代码如下:
gsub(paste0('^(.{', as.integer(loc[1])-1, '})(.+)$'), new_cols, sql)
Run Code Online (Sandbox Code Playgroud)
Run Code Online (Sandbox Code Playgroud)Error in gsub(paste0("^(.{273})(.+)$"), new_cols, sql) : invalid regular expression '^(.{273})(.+)$', reason 'Invalid contents of {}'
当括号中的数字(在这种情况下为273)较小时,此代码可以正常工作,但当它很大时则不行.
这会产生错误:
sql <- "The cat with the bat went to town. He ate the fat mat and wouldn't stop til the sun came up. He was a fat cat that lived with a rat who owned many hats.The cat with the bat went to town. He ate the fat mat and wouldn't stop til the sun came up. …Run Code Online (Sandbox Code Playgroud) 寻找一些有关如何在 R 字符向量列表中用直撇号替换弯撇号的指导。
\n\n我替换大写撇号的原因 - 在脚本的后面,我检查每个列表项,看看是否在字典中找到它(使用 qdapDictionary)以确保它是真正的单词而不是垃圾。字典使用直撇号,因此带有弯撇号的单词将被“拒绝”。
\n\n我当前拥有的代码示例如下。在我的测试列表中,项目#6 包含一个弯撇号,项目#2 包含一个直撇号。
\n\n例子:
\n\nlist_TestWords <- as.list(c("this", "isn\'t", "ideal", "but", "we", "can\xe2\x80\x99t", "fix", "it"))\n\nfunc_ReplaceTypographicApostrophes <- function(x) {\n gsub("\xe2\x80\x99", "\'", x, ignore.case = TRUE)\n }\n\nlist_TestWords_Fixed <- lapply(list_TestWords, func_ReplaceTypographicApostrophes)\nRun Code Online (Sandbox Code Playgroud)\n\n结果:没有变化。第 6 项仍使用大撇号。请参阅下面的输出。
\n\nlist_TestWords_Fixed\n[[1]]\n[1] "this"\n\n[[2]]\n[1] "isn\'t"\n\n[[3]]\n[1] "ideal"\n\n[[4]]\n[1] "but"\n\n[[5]]\n[1] "we"\n\n[[6]]\n[1] "can\xe2\x80\x99t"\n\n[[7]]\n[1] "fix"\n\n[[8]]\n[1] "it"\nRun Code Online (Sandbox Code Playgroud)\n\n我们将非常感谢您提供的任何帮助!
\n我有
txt <- "{a} is to {b} what {c} is to {d}"
key <- c(a='apple', b='banana', c='chair', d='door')
fun <- function(x) key[x]
Run Code Online (Sandbox Code Playgroud)
我想快速转换txt为key:
"apple is to banana what chair is to door"
Run Code Online (Sandbox Code Playgroud)
我知道我可以gsub像这样重复使用(或类似的东西):
for (v in names(key)) txt <- gsub(sprintf('{%s}',v), fun(v), txt, fixed = TRUE)
txt
# [1] "apple is to banana what chair is to door"
Run Code Online (Sandbox Code Playgroud)
但我的txt和key都很长,所以上面是有问题的。我想知道是否有更快的方法,例如:
gsub(sprintf('{%s}',names(key)), key, fixed = TRUE) # Does not work
gsub('\\{(a|b|c|d)\\}', fun(...), txt, …Run Code Online (Sandbox Code Playgroud)