http://datadebrief.blogspot.com/2011/06/10-r-one-liners-to-impress-your-friends.html
Multiply Each Item in a List by 2
| lapply( list ( 1 : 4 ),function(n){n * 2 })
( 1 : 4 ) * 2
|
Sum a List of Numbers
| lapply( list ( 1 : 4 ), sum )
sum (unlist( list ( 1 : 4 )))
sum ( 1 : 4 )
|
Verify if Exists in a String
| wordlist = c( "lambda" , "data" , "plot" , "statistics" , "R" )
tweet = c( "R is an integrated suite of software facilities for data manipulation, calculation and graphical display" )
wordlist[wordlist % in % (c(unlist(strsplit(tweet, ' ' , fixed = T))))]
|
Read in a File
| readLines( "data.file" , n = - 1 )
|
Happy Birthday to You!
| lapply(( 1 : 4 ),function(x){ paste(c( "Happy Birthday to " , ifelse(x! = 3 , "you" , "dearName" )), sep = " ", collapse=" ")})
|
Filter list of numbers
| n = c( 49 , 58 , 76 , 82 , 88 , 90 ); c( list (n[which(n< = 60 )]), list (n[which(n> 60 )]))
|
Fetch and Parse an XML web service
Find minimum (or maximum) in a List
|
lapply( list (c( 14 , 35 , - 7 , 46 , 98 )), min , classes = "numeric" , how = "replace" )
min (unlist( list ( 14 , 35 , - 7 , 46 , 98 )))
min (c( 14 , 35 , - 7 , 46 , 98 ))
max (c( 14 , 35 , - 7 , 46 , 98 ))
|
Parallel Processing
| library(doSMP); w < - startWorkers(workerCount = 4 ); registerDoSMP(w); foreach(i = 1 : 3 ) % dopar % sqrt(i)
|
Sieve of Eratosthenes
| library( 'spuRs' ); primesieve(c(), 2 : 50 )
|
没有评论:
发表评论