Posts

matrix

#converting vector to matrix x<-seq(1,10,1) x # [1]  1  2  3  4  5  6  7  8  9 10 dim(x) #NULL dim(x)<-c(2,5) x #     [,1] [,2] [,3] [,4] [,5] # [1,]    1    3    5    7    9 # [2,]    2    4    6    8   10 dim(x)<-NULL x # [1]  1  2  3  4  5  6  7  8  9 10 y<-matrix(x,2,5) y #     [,1] [,2] [,3] [,4] [,5] # [1,]    1    3    5    7    9 # [2,]    2    4    6    8   10 y<-matrix(x,2,5,byrow = T) y #     [,1] [,2] [,3] [,4] [,5] # [1,]    1    2    3    4    5 # [2,]    6    7    8    9   10 z<-rbind(y,c(11,12...

vector

#Objects in R #Single values are treated as vectors 5 "Hello" TRUE #creating a variable with single value x=5 # a single variable is stored as a vector of length 1 x x[1] #check class of an Object using class() function class(x) numericVector<-rep(1,10) numericVector  #[1] 1 1 1 1 1 1 1 1 1 1 numericVector<-rep(1,10);numericVector (numericVector<-rep(1,10)) class(numericVector) #[1] "numeric" mode(numericVector) #Creating numeric vector using c() function numericVector<-c(1,2,3) numericVector #[1] 1 2 3 #Creating a character vector using c() function characterVector<-c("abc","def","xyz") characterVector #[1] "abc" "def" "xyz" #Creating a logical vector logicalVecor<-c(TRUE,FALSE,FALSE) logicalVecor #[1]  TRUE FALSE FALSE logicalVecor1<-c(T,F,F) logicalVecor1 #[1]  TRUE FALSE FALSE #count number of TRUE in a logical vector sum(logicalVec...
You have been provided with business.RData file from yelp_db. Open this in the R studio. (You should see the data element imported in Global Environment Window.) Address the following queries using R coding. Write all codes in an R script file and submit only as an individual assignment. Q1. (a) View the dataset to know what is stored in the data element. What is the R code executed in the command window? (b) Which data structure is used to store the data? (Is it vector/matrix/list/data.frame?) Find the names of the columns of your data? (c) is_open column in the data element represents whether the business is open or not. Find out how many businesses are open and how many are closed. (Use table() function to do that) table(business$is_open)  (d) How many different attributes are there in attribute column ?names(business$attributes) ncol(business$attributes)  (e) Find the average stars and average review count for all the businesses. Q2. We want to subset the ...
 x=10  if(x==1)  {    print(x)  }else if(x==10)  {    print("ten")  }else  {    print("nothing")  } #[1] "ten"    ifelse(x<5,"yes","no") #[1] "no"  ifelse(x<5,0,1) #[1] 1   # One interesting issue with R when dealing with "else" statement. # Let's try two code snippets to understand this issue # code 1 x=10 if(x==10) {   print(x) }else {   print('wrong') } # Output #  x=10 # > if(x==10) # + { # +   print(x) # + } # [1] 10 # > else # Error: unexpected 'else' in "else" # > { # +   print('wrong') # + } # [1] "wrong" # Code 2  x=10  if(x==10)  {    print(x)  }else  {    print('wrong')  } #[1] 10 # As you can see that in code 1, the else statement starts after }, in this case, R don't know if you have else block or not, hence, an error.
#create a new function mySum<-function(x,y)  {   return(x+y) } mySum(3,4) #[1] 7 #get body of a function - just type the name of the function mySum # function(x,y) # { #   return(x+y) # } #special function to apply any function to multiple rows and columns at once x<-data.frame(x1=seq(1,10,1),x2=2,x3=5) x #    x1 x2 x3 # 1   1  2  5 # 2   2  2  5 # 3   3  2  5 # 4   4  2  5 # 5   5  2  5 # 6   6  2  5 # 7   7  2  5 # 8   8  2  5 # 9   9  2  5 # 10 10  2  5 #apply function by column apply(x,2,mean) #  x1  x2  x3 # 5.5 2.0 5.0 #apply function by row apply(x,1,mean) # [1] 2.666667 3.000000 3.333333 3.666667 4.000000 4.333333 4.666667 5.000000 5.333333 5.666667 age=seq(11,20,1) age #[1] 11 12 13 14 15 16...

twitter

library('twitteR') library('httr') #go to https://apps.twitter.com/ and register a new app. #click on app #click on keys and access tokens tab # you will get following credentials from twitter #consumer key #consumer secret #Access token #access token secret #setup OAuth setup_twitter_oauth(consumer_key="*****",                     consumer_secret="******",                     access_token="****",                     access_secret="*****") #Search for tweets containing hashtag #chennai chennai = searchTwitter("#chennai", n=5000) ### find how many tweets are retweets of other tweets

conditional

#special function to apply any function to multiple rows and columns at once x<-data.frame(x1=seq(1,10,1),x2=2,x3=5) x #    x1 x2 x3 # 1   1  2  5 # 2   2  2  5 # 3   3  2  5 # 4   4  2  5 # 5   5  2  5 # 6   6  2  5 # 7   7  2  5 # 8   8  2  5 # 9   9  2  5 # 10 10  2  5 #apply function by column apply(x,2,mean) #  x1  x2  x3 # 5.5 2.0 5.0 #apply function by row apply(x,1,mean) # [1] 2.666667 3.000000 3.333333 3.666667 4.000000 4.333333 4.666667 5.000000 5.333333 5.666667 age=seq(11,20,1) age #[1] 11 12 13 14 15 16 17 18 19 20 m_name=letters[1:10] m_name # [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" rbind(m_name,age) #        [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] # m_name "a"  "b"  "c"  "d...