仅当它是 R 中的特定字符时才替换字符串中的第 n 个字符
replacing the nth character in a string only if it is a particular character in R
我正在将一系列调查导入为 .csv 文件并合并到一个数据集中。问题是对于七个文件之一,一些变量的导入略有不同。数据集很大,我想找到一种方法来为 运行 编写一个函数,覆盖给我带来麻烦的数据集。
在一些变量中应该有一个点的时候有一个下划线。并非所有变量都具有相同的格式,但不正确的是,因为下划线始终是列名的第 6 个元素。
我希望 R 查找第 6 个元素,如果它是下划线,则将其替换为点。下面是一个虚构的例子。
col_names <- c("s1.help_needed",
"s1.Q2_im_stuck",
"s1.Q2.im_stuck",
"s1.Q3.regex",
"s1.Q3_regex",
"s2.Q1.is_confusing",
"s2.Q2.answer_please",
"s2.Q2_answer_please",
"s2.someone_knows_the answer",
"s3.appreciate_the_help")
我假设有一个正则表达式答案,但我正在努力寻找一个。也许还有一个整洁的答案?
正如@thelatemail 指出的那样,none 您的数据实际上在第五位有下划线,但有些在第六位有下划线(其他人有点)。基本的 R 方法是使用 gsub()
:
result <- gsub("^(.{5})_", "\1.", col_names)
> result
[1] "s1.help_needed" "s1.Q2.im_stuck"
[3] "s1.Q2.im_stuck" "s1.Q3.regex"
[5] "s1.Q3.regex" "s2.Q1.is_confusing"
[7] "s2.Q2.answer_please" "s2.Q2.answer_please"
[9] "s2.someone_knows_the answer" "s3.appreciate_the_help"
下面是对正则表达式的解释:
^ from the start of the string
(.{5}) match AND capture any five characters
_ followed by an underscore
括号内的数量称为捕获组,可用于\1
替换。所以正则表达式是说用我们捕获的五个字符替换前六个字符,但使用一个点作为第六个字符。
您可以使用由任何类型的前 4 个(实际上是 5 个)字符后跟下划线定义的 "capture-class",并替换为后跟 "dot" 的任何这 5 个字符。由于所有示例的第 6 个位置都有下划线,我猜您没有计算原始 "dots":
> col_names
[1] "s1.help_needed" "s1.Q2_im_stuck"
[3] "s1.Q2.im_stuck" "s1.Q3.regex"
[5] "s1.Q3_regex" "s2.Q1.is_confusing"
[7] "s2.Q2.answer_please" "s2.Q2_answer_please"
[9] "s2.someone_knows_the answer" "s3.appreciate_the_help"
> sub("^(.....)_", "\1.", col_names)
[1] "s1.help.needed" "s1.Q2.im_stuck"
[3] "s1.Q2.im.stuck" "s1.Q3.regex"
[5] "s1.Q3.regex" "s2.Q1.is.confusing"
[7] "s2.Q2.answer.please" "s2.Q2.answer_please"
[9] "s2.someone.knows_the answer" "s3.appreciate.the_help"
由于替换参数不存在与转义相同的问题,因此您不需要像在 R-regex 模式参数中使用的那样使用双反斜杠。
我正在将一系列调查导入为 .csv 文件并合并到一个数据集中。问题是对于七个文件之一,一些变量的导入略有不同。数据集很大,我想找到一种方法来为 运行 编写一个函数,覆盖给我带来麻烦的数据集。
在一些变量中应该有一个点的时候有一个下划线。并非所有变量都具有相同的格式,但不正确的是,因为下划线始终是列名的第 6 个元素。
我希望 R 查找第 6 个元素,如果它是下划线,则将其替换为点。下面是一个虚构的例子。
col_names <- c("s1.help_needed",
"s1.Q2_im_stuck",
"s1.Q2.im_stuck",
"s1.Q3.regex",
"s1.Q3_regex",
"s2.Q1.is_confusing",
"s2.Q2.answer_please",
"s2.Q2_answer_please",
"s2.someone_knows_the answer",
"s3.appreciate_the_help")
我假设有一个正则表达式答案,但我正在努力寻找一个。也许还有一个整洁的答案?
正如@thelatemail 指出的那样,none 您的数据实际上在第五位有下划线,但有些在第六位有下划线(其他人有点)。基本的 R 方法是使用 gsub()
:
result <- gsub("^(.{5})_", "\1.", col_names)
> result
[1] "s1.help_needed" "s1.Q2.im_stuck"
[3] "s1.Q2.im_stuck" "s1.Q3.regex"
[5] "s1.Q3.regex" "s2.Q1.is_confusing"
[7] "s2.Q2.answer_please" "s2.Q2.answer_please"
[9] "s2.someone_knows_the answer" "s3.appreciate_the_help"
下面是对正则表达式的解释:
^ from the start of the string
(.{5}) match AND capture any five characters
_ followed by an underscore
括号内的数量称为捕获组,可用于\1
替换。所以正则表达式是说用我们捕获的五个字符替换前六个字符,但使用一个点作为第六个字符。
您可以使用由任何类型的前 4 个(实际上是 5 个)字符后跟下划线定义的 "capture-class",并替换为后跟 "dot" 的任何这 5 个字符。由于所有示例的第 6 个位置都有下划线,我猜您没有计算原始 "dots":
> col_names
[1] "s1.help_needed" "s1.Q2_im_stuck"
[3] "s1.Q2.im_stuck" "s1.Q3.regex"
[5] "s1.Q3_regex" "s2.Q1.is_confusing"
[7] "s2.Q2.answer_please" "s2.Q2_answer_please"
[9] "s2.someone_knows_the answer" "s3.appreciate_the_help"
> sub("^(.....)_", "\1.", col_names)
[1] "s1.help.needed" "s1.Q2.im_stuck"
[3] "s1.Q2.im.stuck" "s1.Q3.regex"
[5] "s1.Q3.regex" "s2.Q1.is.confusing"
[7] "s2.Q2.answer.please" "s2.Q2.answer_please"
[9] "s2.someone.knows_the answer" "s3.appreciate.the_help"
由于替换参数不存在与转义相同的问题,因此您不需要像在 R-regex 模式参数中使用的那样使用双反斜杠。