Rにおける基本動作01

3998 ワード

謝益輝先生のR言語忍者の秘訣の基本的な操作を繰り返します
Chard Liu

2019 1 8 

#Sys.setlocale(‘LC_ALL’,‘C’) readlines      ,       

# R        (GPL)
gpl = readLines(file.path(R.home(), "COPYING"))
head(gpl)  # GPL   
## [1] "\t\t    GNU GENERAL PUBLIC LICENSE"                                             
## [2] "\t\t       Version 2, June 1991"                                                
## [3] ""                                                                               
## [4] " Copyright (C) 1989, 1991 Free Software Foundation, Inc."                       
## [5] "                       51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA"
## [6] " Everyone is permitted to copy and distribute verbatim copies"
xie = readLines("https://yihui.name")  #     
head(xie)  # HTML  
## [1] ""                                                             
## [2] ""                                                       
## [3] "  "                                                                    
## [4] "\t"                       
## [5] "    "                                                
## [6] "    "
nchar(gpl[1:10])  # GPL 10        
##  [1] 32 29  0 56 79 61 58  0 15  0
sum(nchar(gpl))
## [1] 17671
#strsplit        ,       
strsplit(gpl[4:5], " ")  #    4、5  
## [[1]]
## [1] ""            "Copyright"   "(C)"         "1989,"       "1991"       
## [6] "Free"        "Software"    "Foundation," "Inc."       
## 
## [[2]]
##  [1] ""           ""           ""           ""           ""          
##  [6] ""           ""           ""           ""           ""          
## [11] ""           ""           ""           ""           ""          
## [16] ""           ""           ""           ""           ""          
## [21] ""           ""           ""           "51"         "Franklin"  
## [26] "St,"        "Fifth"      "Floor,"     "Boston,"    "MA"        
## [31] ""           "02110-1301" ""           "USA"
#           ,              
#           
#        ,                \\W(         W),     ##              ,            
words = unlist(strsplit(gpl, "\\W"))#unlist      ,    
words = words[words != ""]  #      
#      10   
tail(sort(table(tolower(words))), 10)#tolower    ,sort  ,tail    
## 
##    this      is       a program     and     you      or      of      to 
##      49      53      57      71      72      76      77     104     108 
##     the 
##     194
#             substr substring
xie[8]
## [1] "    Yihui Xie |  ㈢  \x89"
substr(xie[8], 12, 20)#  12-20   
## [1] "Yihui Xie"
#    
paste(1:3,"a")
## [1] "1 a" "2 a" "3 a"
paste(1:3,"a",sep = "-")#   a -  ,       
## [1] "1-a" "2-a" "3-a"
paste(letters[1:10], collapse = "~")#collapase              
## [1] "a~b~c~d~e~f~g~h~i~j"
paste(1:3, "a", sep = "-", collapse = "+")#    
## [1] "1-a+2-a+3-a"
#sep       , collapse     “  ”      
      ,        

love = function() cat("I love you
")#
function , say = function(person) { love() love() cat(paste("I love you dear", person, "
")) love() } say("somebody") # somebody ## I love you ## I love you ## I love you dear somebody ## I love you