R to check the sequence of ALL CAPS in each line of the file

There is a csv data file that is populated with lots of raw data as follows:

data.frame( id=1:4, data=c( "it a programming language", "this data is JUNK", "refer www.google.com", "check for more information") ) 

I need to process this data and check the ALL CAPS sequence for each row and populate a new column with a 0/1 entry.

The output file is as follows:

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

How to achieve this with R? I searched for this for a while, unable to find any fruitful results for processing each row.

+4
source share
1 answer

Assuming your data.frame is called test :

 test$all_caps <- grepl("[AZ]{2,}",test$data) id data all_caps 1 1 it 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 

What can you do 0 and 1 by calling as.numeric

 test$all_caps <- as.numeric(grepl("[AZ]{2,}",test$data)) id data all_caps 1 1 it 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 
+7
source

Source: https://habr.com/ru/post/1496494/


All Articles