Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从R中的write.table读取.table_R_Gsub_Read.table_Write.table_Qdap - Fatal编程技术网

从R中的write.table读取.table

从R中的write.table读取.table,r,gsub,read.table,write.table,qdap,R,Gsub,Read.table,Write.table,Qdap,我正在尝试做一个qdap::multigsub,以修复气候事件类型列表中的一些打字错误、拼写错误的名称、变体表达式和其他一些“异常”(是的,这是美国国家海洋和大气管理局关于风暴的数据集,属于coursera可再生研究课程中的一项作业;尽管这项作业既不要求也不期望这样做:这是我在竭尽全力!) 所以我有一些事件被命名为“山洪暴发”、“山洪暴发”、“山洪暴发”等等,我想把它们都归为“山洪暴发”级别。所以我首先做的是: expr <- c("^flash.*floo.*","thun.*") re

我正在尝试做一个
qdap::multigsub
,以修复气候事件类型列表中的一些打字错误、拼写错误的名称、变体表达式和其他一些“异常”(是的,这是美国国家海洋和大气管理局关于风暴的数据集,属于coursera可再生研究课程中的一项作业;尽管这项作业既不要求也不期望这样做:这是我在竭尽全力!)

所以我有一些事件被命名为“山洪暴发”、“山洪暴发”、“山洪暴发”等等,我想把它们都归为“山洪暴发”级别。所以我首先做的是:

expr <- c("^flash.*floo.*","thun.*")
repl <- c("flash flood","thunderstorm")
这使得代码有点混乱。当然,我有完整的
expr
repl
向量,所以我希望将对应值的每一对(expr和repl)放在一行中,这样代码的读者就可以轻松阅读(这就是为什么
dput
在这里不起作用的原因:它们不会对齐每一对值)

我试过这个:

a <- data.frame(expr=expr,repl=repl)
print(a,rownames=FALSE)
  # copying the output, and then
b <- read.table(header=TRUE,text="paste_text_here")
但它也不起作用(我认为是因为
write.table
在引号之间输出每个项目,而
read.table
发现太多引号无法处理)

我希望在我的Rmarkdown文件中包含如下内容:

exprRepl <- read.table(header=TRUE,text="expr repl
                                         expr_1 repl_1
                                         expr_2 repl_2")

如果有任何其他方法来替换错误的/变体名称,我很高兴听到并尝试一下!

一个解决方案是在粘贴的文本周围使用一个单独的引号
(只要数据中没有
,这就行):


d非常简单的解决方案,谢谢!。这是在
?引号中完成的(我甚至没有想到要咨询!):“单引号通常只用于分隔包含双引号的字符常量。”。
write.table(a,rownames=FALSE)
  # copying the output, and then
b <- read.table(header=TRUE,text="paste_text_here")
exprRepl <- read.table(header=TRUE,text="expr repl
                                         expr_1 repl_1
                                         expr_2 repl_2")
> dput(a[1:5,])
structure(list(expr = structure(c(5L, 1L, 2L, 3L, 4L), .Label = c("^BLIZZARD.*", 
"^FLASH.*FLOOD.*", "^HAIL.*", "^HEAVY.*RAIN.*", "^HURRICANE.*"
), class = "factor"), repl = structure(c(5L, 1L, 2L, 3L, 4L), .Label = c("BLIZZARD", 
"FLASH FLOOD", "HAIL", "HEAVY RAIN", "HURRICANE"), class = "factor")), .Names = c("expr", 
"repl"), row.names = c(NA, 5L), class = "data.frame")
d <- structure(list(expr = structure(c(5L, 1L, 2L, 3L, 4L), .Label = c("^BLIZZARD.*", 
"^FLASH.*FLOOD.*", "^HAIL.*", "^HEAVY.*RAIN.*", "^HURRICANE.*"
), class = "factor"), repl = structure(c(5L, 1L, 2L, 3L, 4L), .Label = c("BLIZZARD", 
"FLASH FLOOD", "HAIL", "HEAVY RAIN", "HURRICANE"), class = "factor")), .Names = c("expr", 
"repl"), row.names = c(NA, 5L), class = "data.frame")

write.table(d, row.names=FALSE)

# copy paste output of write.table in text field below:
read.table(header = TRUE, text='"expr" "repl"
"^HURRICANE.*" "HURRICANE"
"^BLIZZARD.*" "BLIZZARD"
"^FLASH.*FLOOD.*" "FLASH FLOOD"
"^HAIL.*" "HAIL"
"^HEAVY.*RAIN.*" "HEAVY RAIN"')