Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.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替换字符_R - Fatal编程技术网

使用R替换字符

使用R替换字符,r,R,我有一个csv文件,有一列,地址。它有如下价值: A <- structure(list(Address = structure(1:3, .Label = c("&2340 P St", "&5656 N St", "456 B Street"), class = "factor")), .Names = "Address", row.names = c(NA, 3L), class = "data.frame") A ## Address ## 1

我有一个csv文件,有一列,地址。它有如下价值:

A <- structure(list(Address = structure(1:3, .Label = c("&2340 P St", 
 "&5656 N St", "456 B Street"), class = "factor")), .Names = "Address", 
 row.names = c(NA, 3L), class = "data.frame")
A
##        Address
## 1   &2340 P St
## 2   &5656 N St
## 3 456 B Street
代码如下:

 A <-read.csv("U:/161/1.csv", header=T,sep=",")
 B<-gsub("&", " ", A$ADDRESS1, ignore.case = TRUE)
 write.table(B, file = "U:/161/2.csv", sep = ","
 , col.names = NA, qmethod = "double")

A使用
grep
grepl
识别哪些行包含
&
,然后排除这些行

B <- droplevels(A[!grepl('&', A$Address), ,drop=FALSE])
B
##       Address
## 3 456 B Street

您希望删除的其余地址是什么?不应该保留前两个条目?如果你想删除带有
&
的行,你可以使用
grep('&',一个$Address)
Ah,这可能是OP所追求的。如果我想用'&'替换所有开始的内容以清空记录,该怎么办。我应该使用gsub吗?
grep
grepl
都可以,语法略有不同。请参阅我的编辑您可能有一个名为
F
的变量,使用
drop=FALSE应该可以工作
B <- droplevels(A[!grepl('&', A$Address), ,drop=FALSE])
B
##       Address
## 3 456 B Street
B <- droplevels(A[-grep('&', A$Address), ,drop=FALSE])
B