Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/79.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命令,用于检查文件中每一行的ALL CAPS序列_R - Fatal编程技术网

R命令,用于检查文件中每一行的ALL CAPS序列

R命令,用于检查文件中每一行的ALL CAPS序列,r,R,有一个csv数据文件,其中包含大量原始数据,如下所示: data.frame( id=1:4, data=c( "it's a programming language", "this data is JUNK", "refer www.google.com", "check for more information") ) 我需要处理这些数据,检查每一行的ALL CAPS序列,并用0/1条目填充一个新列 输出文件

有一个csv数据文件,其中包含大量原始数据,如下所示:

data.frame(
  id=1:4,
  data=c(
         "it's a programming language",
         "this data is JUNK",
         "refer www.google.com",
         "check for more information")
)
我需要处理这些数据,检查每一行的ALL CAPS序列,并用0/1条目填充一个新列

输出文件如下所示:

id  data                         all_caps
1   it's a programming language         0
2   this data is JUNK                   1
3   refer www.google.com                0
4   check for more information          0

如何用R实现这一点?我已经为此搜索了一段时间,没有找到任何关于处理每一行的有效结果。

假设您的data.frame被称为
test

test$all_caps <- grepl("[A-Z]{2,}",test$data)

  id                        data all_caps
1  1 it's a programming language    FALSE
2  2           this data is JUNK     TRUE
3  3        refer www.google.com    FALSE
4  4  check for more information    FALSE

您可能希望
“[A-Z]{2,}”
匹配2个或更多大写字母;1资本不应该算作这个用途。@HongOoi-好的捕获-我以为我已经用
+
覆盖了它,但显然不是。太好了!这救了我一天。非常感谢。
test$all_caps <- as.numeric(grepl("[A-Z]{2,}",test$data))

  id                        data all_caps
1  1 it's a programming language        0
2  2           this data is JUNK        1
3  3        refer www.google.com        0
4  4  check for more information        0