DataFrame Demo

a <- 3
b <- 2
a + b
## [1] 5
# Download File
#download.file('https://raw.githubusercontent.com/ywchiu/fetnetpy/master/data/rent_591_sample.csv', 'rent_591_sample.csv')

# Get working directory
getwd()
## [1] "D:/OS DATA/Desktop"
# Read Data
library(readr)
rent_591_sample <- read_csv("D:/OS DATA/Desktop/rent_591_sample.csv")
## Warning: Missing column names filled in: 'X1' [1]
## Parsed with column specification:
## cols(
##   X1 = col_integer(),
##   detail_url = col_character(),
##   search_date = col_date(format = ""),
##   title = col_character(),
##   address = col_character(),
##   floor_info = col_character(),
##   price = col_character(),
##   layout = col_character(),
##   building_area = col_double(),
##   building_use = col_character(),
##   latitude = col_double(),
##   longitude = col_double()
## )
head(rent_591_sample)
## # A tibble: 6 x 12
##      X1                                       detail_url search_date
##   <int>                                            <chr>      <date>
## 1     0 https://rent.591.com.tw/rent-detail-1050934.html  2017-05-14
## 2     1 https://rent.591.com.tw/rent-detail-1008046.html  2017-05-14
## 3     2 https://rent.591.com.tw/rent-detail-1586266.html  2017-05-12
## 4     3 https://rent.591.com.tw/rent-detail-1699948.html  2017-05-13
## 5     4 https://rent.591.com.tw/rent-detail-1404725.html  2017-05-12
## 6     5 https://rent.591.com.tw/rent-detail-1633388.html  2017-05-12
## # ... with 9 more variables: title <chr>, address <chr>, floor_info <chr>,
## #   price <chr>, layout <chr>, building_area <dbl>, building_use <chr>,
## #   latitude <dbl>, longitude <dbl>
class(rent_591_sample)
## [1] "tbl_df"     "tbl"        "data.frame"
str(rent_591_sample)
## Classes 'tbl_df', 'tbl' and 'data.frame':    70315 obs. of  12 variables:
##  $ X1           : int  0 1 2 3 4 5 6 7 8 9 ...
##  $ detail_url   : chr  "https://rent.591.com.tw/rent-detail-1050934.html" "https://rent.591.com.tw/rent-detail-1008046.html" "https://rent.591.com.tw/rent-detail-1586266.html" "https://rent.591.com.tw/rent-detail-1699948.html" ...
##  $ search_date  : Date, format: "2017-05-14" "2017-05-14" ...
##  $ title        : chr  "套房出租-近東方學院(六~八月可入住)" "好房子【聰明的你、妳快來租吧】" "陽台大套房(獨立洗衣機)近頂溪捷運站" "火車站附近~~優質套房" ...
##  $ address      : chr  "高雄市湖內區民生街" "新北市蘆洲區長安街" "新北市永和區中山路一段" "屏東縣潮州鎮六合路" ...
##  $ floor_info   : chr  "3F/3F" "4F/5F" "8F/12F" "3F/3F" ...
##  $ price        : chr  "4,500元/月" "7,500元/月" "11,000元/月" "5,000元/月" ...
##  $ layout       : chr  NA NA NA NA ...
##  $ building_area: num  6 10 8 8 7 24 7 4 9 32 ...
##  $ building_use : chr  "透天厝/獨立套房" "透天厝/獨立套房" "電梯大樓/分租套房" "透天厝/獨立套房" ...
##  $ latitude     : num  22.9 25.1 25 22.6 25 ...
##  $ longitude    : num  120 121 122 121 121 ...
##  - attr(*, "spec")=List of 2
##   ..$ cols   :List of 12
##   .. ..$ X1           : list()
##   .. .. ..- attr(*, "class")= chr  "collector_integer" "collector"
##   .. ..$ detail_url   : list()
##   .. .. ..- attr(*, "class")= chr  "collector_character" "collector"
##   .. ..$ search_date  :List of 1
##   .. .. ..$ format: chr ""
##   .. .. ..- attr(*, "class")= chr  "collector_date" "collector"
##   .. ..$ title        : list()
##   .. .. ..- attr(*, "class")= chr  "collector_character" "collector"
##   .. ..$ address      : list()
##   .. .. ..- attr(*, "class")= chr  "collector_character" "collector"
##   .. ..$ floor_info   : list()
##   .. .. ..- attr(*, "class")= chr  "collector_character" "collector"
##   .. ..$ price        : list()
##   .. .. ..- attr(*, "class")= chr  "collector_character" "collector"
##   .. ..$ layout       : list()
##   .. .. ..- attr(*, "class")= chr  "collector_character" "collector"
##   .. ..$ building_area: list()
##   .. .. ..- attr(*, "class")= chr  "collector_double" "collector"
##   .. ..$ building_use : list()
##   .. .. ..- attr(*, "class")= chr  "collector_character" "collector"
##   .. ..$ latitude     : list()
##   .. .. ..- attr(*, "class")= chr  "collector_double" "collector"
##   .. ..$ longitude    : list()
##   .. .. ..- attr(*, "class")= chr  "collector_double" "collector"
##   ..$ default: list()
##   .. ..- attr(*, "class")= chr  "collector_guess" "collector"
##   ..- attr(*, "class")= chr "col_spec"
head(rent_591_sample$price)
## [1] "4,500元/月"  "7,500元/月"  "11,000元/月" "5,000元/月"  "5,000元/月" 
## [6] "22,000元/月"
class(rent_591_sample$price)
## [1] "character"
# 資料清理
rent_591_sample$price <- gsub('元/月', '', rent_591_sample$price)
rent_591_sample$price <- gsub(',', '', rent_591_sample$price)
rent_591_sample$price <- as.integer(rent_591_sample$price)

# 敘述性統計
mean(rent_591_sample$price)
## [1] 37019.23
median(rent_591_sample$price)
## [1] 14000
max(rent_591_sample$price)
## [1] 12750000
min(rent_591_sample$price)
## [1] 500
summary(rent_591_sample$price)
##     Min.  1st Qu.   Median     Mean  3rd Qu.     Max. 
##      500     6800    14000    37019    32000 12750000
# 資料排序
head(sort(rent_591_sample$price))
## [1]  500  700  800  980 1000 1000
head(sort(rent_591_sample$price, decreasing = TRUE))
## [1] 12750000  9579496  8000000  7000000  6198712  5500000
head(order(rent_591_sample$price, decreasing = TRUE))
## [1] 31659  9816 69808 40965  9964 69922
rent_591_sample[order(rent_591_sample$price, decreasing = TRUE), ]
## # A tibble: 70,315 x 12
##       X1                                       detail_url search_date
##    <int>                                            <chr>      <date>
##  1 31658 https://rent.591.com.tw/rent-detail-5133302.html  2017-05-14
##  2  9815 https://rent.591.com.tw/rent-detail-5047313.html  2017-05-15
##  3 69807 https://rent.591.com.tw/rent-detail-5191373.html  2017-05-13
##  4 40964 https://rent.591.com.tw/rent-detail-5148539.html  2017-05-14
##  5  9963 https://rent.591.com.tw/rent-detail-5047316.html  2017-05-15
##  6 69921 https://rent.591.com.tw/rent-detail-5191625.html  2017-05-13
##  7  1404 https://rent.591.com.tw/rent-detail-4732277.html  2017-05-10
##  8 22729 https://rent.591.com.tw/rent-detail-5113471.html  2017-05-09
##  9 69997 https://rent.591.com.tw/rent-detail-5191597.html  2017-05-13
## 10  8909 https://rent.591.com.tw/rent-detail-5047317.html  2017-05-15
## # ... with 70,305 more rows, and 9 more variables: title <chr>,
## #   address <chr>, floor_info <chr>, price <int>, layout <chr>,
## #   building_area <dbl>, building_use <chr>, latitude <dbl>,
## #   longitude <dbl>
# 篩選物件
rent_591_sample$building_use <- as.factor(rent_591_sample$building_use)
levels(rent_591_sample$building_use)
##  [1] "公寓/分租套房"     "公寓/住辦"         "公寓/其他"        
##  [4] "公寓/店面"         "公寓/雅房"         "公寓/整層住家"    
##  [7] "公寓/獨立套房"     "公寓/辦公"         "別墅/分租套房"    
## [10] "別墅/住辦"         "別墅/其他"         "別墅/店面"        
## [13] "別墅/雅房"         "別墅/整層住家"     "別墅/獨立套房"    
## [16] "別墅/辦公"         "其他/其他"         "倉庫/其他"        
## [19] "透天厝/分租套房"   "透天厝/住辦"       "透天厝/其他"      
## [22] "透天厝/店面"       "透天厝/雅房"       "透天厝/整層住家"  
## [25] "透天厝/獨立套房"   "透天厝/辦公"       "電梯大樓/分租套房"
## [28] "電梯大樓/住辦"     "電梯大樓/其他"     "電梯大樓/店面"    
## [31] "電梯大樓/雅房"     "電梯大樓/整層住家" "電梯大樓/獨立套房"
## [34] "電梯大樓/辦公"     "廠房/其他"         "廠辦/其他"
apartment <- rent_591_sample[rent_591_sample$building_use == "公寓/獨立套房", 'price']
summary(apartment)
##      price      
##  Min.   : 2000  
##  1st Qu.: 6000  
##  Median : 7500  
##  Mean   : 8299  
##  3rd Qu.:10000  
##  Max.   :48000  
##  NA's   :4985
apartment <- rent_591_sample[rent_591_sample$building_use == "公寓/獨立套房", c('price', 'building_area') ]
apartment$price_per_pin <- apartment$price / apartment$building_area

summary(apartment$price_per_pin)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max.    NA's 
##   233.3   812.5  1045.5  1119.3  1357.1  4285.7    4985
head(rent_591_sample$address)
## [1] "高雄市湖內區民生街"     "新北市蘆洲區長安街"    
## [3] "新北市永和區中山路一段" "屏東縣潮州鎮六合路"    
## [5] "桃園市觀音區四維路"     "新北市淡水區民族路"
rent_591_sample$city <- substr(rent_591_sample$address, 1,3)

taipei_apartment <- rent_591_sample[rent_591_sample$building_use == "公寓/獨立套房" & rent_591_sample$city=='台北市', c('price', 'building_area') ]

summary(taipei_apartment$price)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max.    NA's 
##    4500    9000   11000   11686   13500   35000    1593
taipei_apartment$price_per_pin <- taipei_apartment$price / taipei_apartment$building_area

summary(taipei_apartment$price_per_pin)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max.    NA's 
##   583.3  1266.7  1500.0  1552.7  1760.0  3900.0    1593
taichung_apartment <- rent_591_sample[rent_591_sample$building_use == "公寓/獨立套房" & rent_591_sample$city=='台中市', c('price', 'building_area') ]
summary(taichung_apartment$price)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max.    NA's 
##    3000    5000    6083    6543    7300   48000     667
taichung_apartment$price_per_pin <- taichung_apartment$price / taichung_apartment$building_area

summary(taichung_apartment$price_per_pin)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max.    NA's 
##   250.0   700.0   812.5   835.2   937.5  4000.0     667

Lists

item <- list(thing='hat',size = 8.25)
item
## $thing
## [1] "hat"
## 
## $size
## [1] 8.25
item$thing
## [1] "hat"
item$size
## [1] 8.25
item$size * 10
## [1] 82.5
test <- list(name= 'toby', score = c(87,57,72))
test
## $name
## [1] "toby"
## 
## $score
## [1] 87 57 72
test$score[2]
## [1] 57
li <- list(c(3,5,12), c(2,4,5,8,10))
li[[1]]
## [1]  3  5 12
li[[2]]
## [1]  2  4  5  8 10
sum(li[[1]])
## [1] 20
sum(li[[2]])
## [1] 29
lapply(li, sum)
## [[1]]
## [1] 20
## 
## [[2]]
## [1] 29

File Read and Write

download.file('https://raw.githubusercontent.com/ywchiu/fubonr/master/data/match.txt', 'match.txt')

getwd()
## [1] "D:/OS DATA/Desktop"
#?read.table

test.data <- read.table('match.txt', sep = '|', header = FALSE)


download.file('https://raw.githubusercontent.com/ywchiu/fubonr/master/data/2330.csv', '2330.csv')

test.data <- read.csv('2330.csv', header = TRUE)
class(test.data)
## [1] "data.frame"
library(readr)
test.data <- read_csv('2330.csv')
## Parsed with column specification:
## cols(
##   Date = col_date(format = ""),
##   Open = col_double(),
##   High = col_double(),
##   Low = col_double(),
##   Close = col_double(),
##   `Adj Close` = col_double(),
##   Volume = col_integer()
## )
test.data
## # A tibble: 372 x 7
##          Date  Open  High   Low Close `Adj Close`   Volume
##        <date> <dbl> <dbl> <dbl> <dbl>       <dbl>    <int>
##  1 2016-03-17 159.5 160.0 157.5 158.5    147.5989 48193000
##  2 2016-03-18 158.5 159.5 158.5 159.5    148.5301 55975000
##  3 2016-03-21 160.0 160.0 158.0 160.0    148.9957 26100000
##  4 2016-03-22 159.5 159.5 157.0 158.5    147.5989 25809000
##  5 2016-04-12 157.5 159.5 157.5 158.5    147.5989 14859000
##  6 2016-04-13 160.0 162.0 159.5 161.5    150.3926 39611000
##  7 2016-04-14 162.0 162.5 160.0 161.5    150.3926 31338000
##  8 2016-04-15 157.0 160.0 156.0 159.5    148.5301 48779000
##  9 2016-04-18 158.0 158.5 157.5 158.0    147.1333 20279000
## 10 2016-04-19 160.0 161.0 157.0 157.5    146.6677 26577000
## # ... with 362 more rows
write.table(x = test.data, file = '2330.tsv', sep = '\t')
write.csv(x = test.data, file = '2330_2.csv')


 save.image("D:/OS DATA/Desktop/env20171113.RData")
 load("D:/OS DATA/Desktop/env20171113.RData")
 
 fit <- lm(price ~ building_area, data = taipei_apartment)
 fit
## 
## Call:
## lm(formula = price ~ building_area, data = taipei_apartment)
## 
## Coefficients:
##   (Intercept)  building_area  
##        5371.4          800.1
 save(x=fit, file = 'regression.RData')
 load('regression.RData')

Flow Control

x <- 5
x > 3
## [1] TRUE
x > 6
## [1] FALSE
x <- 2
if (x > 3){
  print("x > 3")
} else{
  print("x <= 3")
}
## [1] "x <= 3"
x <- 2
if(x > 3){
  print("x > 3")
}else if (x == 3){
  print("x = 3")
}else{
  print("x < 3")
  print("END")
}
## [1] "x < 3"
## [1] "END"
#1:100

1:10
##  [1]  1  2  3  4  5  6  7  8  9 10
for (i in 1:10){
  print(i)
}
## [1] 1
## [1] 2
## [1] 3
## [1] 4
## [1] 5
## [1] 6
## [1] 7
## [1] 8
## [1] 9
## [1] 10
financial_rpt <- c(10,5,8,9,12)
for(rec in financial_rpt){
  print(rec)
}
## [1] 10
## [1] 5
## [1] 8
## [1] 9
## [1] 12
s <- 0 
for(i in 1:100){
  s <- s+i
}
s
## [1] 5050
sum(1:100)
## [1] 5050
x <- c('sunny', 'rainy', 'cloudy', 'rainy', 'cloudy')
length(x)
## [1] 5
for(i in 1:length(x)){
  #print(i)
  print(x[i])
}
## [1] "sunny"
## [1] "rainy"
## [1] "cloudy"
## [1] "rainy"
## [1] "cloudy"
for( i in seq_along(x)){
  print(x[i])
}
## [1] "sunny"
## [1] "rainy"
## [1] "cloudy"
## [1] "rainy"
## [1] "cloudy"
for (i in x){
  print(i)
}
## [1] "sunny"
## [1] "rainy"
## [1] "cloudy"
## [1] "rainy"
## [1] "cloudy"
s <- 0
cnt <- 0
while(cnt <= 100){
  s <- s + cnt
  cnt <- cnt + 1
}
s
## [1] 5050
news <- 'https://tw.appledaily.com/new/realtime/'

paste(news, '1')
## [1] "https://tw.appledaily.com/new/realtime/ 1"
paste(news, '1', sep='')
## [1] "https://tw.appledaily.com/new/realtime/1"
paste0(news, '1')
## [1] "https://tw.appledaily.com/new/realtime/1"
?paste
## starting httpd help server ... done
for(i in 1:3){
  print(paste0(news,i))
}
## [1] "https://tw.appledaily.com/new/realtime/1"
## [1] "https://tw.appledaily.com/new/realtime/2"
## [1] "https://tw.appledaily.com/new/realtime/3"

Function

(72 ^ (1/2)) * 10
## [1] 84.85281
(56 ^ (1/2)) * 10
## [1] 74.83315
c(72,56) ^(1/2) * 10
## [1] 84.85281 74.83315
f <- function(ele){
  res <- ele ^ (1/2) * 10
  return(res)
}

f <- function(ele){
  res <- ele ^ (1/2) * 10
  res
}

f(72)
## [1] 84.85281
f(c(52,72,86,90,40))
## [1] 72.11103 84.85281 92.73618 94.86833 63.24555
addNum <- function(a, b){
  a + b
}

addNum(3,5)
## [1] 8
addNum <- function(a, b=100){
  a + b
}
addNum(3,5)
## [1] 8
addNum(10)
## [1] 110
f <- function(a,b){
  a * 2
}
f(5)
## [1] 10
f(5,7)
## [1] 10
f <- function(a,b){
  a + b
}

f(2,3)
## [1] 5
#f(2)

cal <- function(a,b){
  a + b * 10
}

cal(3,2)
## [1] 23
cal(a = 3, b = 2)
## [1] 23
cal(2,3)
## [1] 32
cal(b =2, a = 3)
## [1] 23
h <- c(180,169,172,166,162,180,177)

head(h)
## [1] 180 169 172 166 162 180
head(h,n =3)
## [1] 180 169 172
head(h,3)
## [1] 180 169 172
#head(3,h)
head(n = 3, x = h)
## [1] 180 169 172
?head

?paste
paste('Hello', 'World')
## [1] "Hello World"
paste('Hello', 'World', 'hi', 'yo', 'ok')
## [1] "Hello World hi yo ok"
paste('Hello', 'World', 'hi', 'yo', 'ok', sep= '|')
## [1] "Hello|World|hi|yo|ok"
seq(1,10, by = 1)
##  [1]  1  2  3  4  5  6  7  8  9 10
seq(1,10, by = 2)
## [1] 1 3 5 7 9
seq(1,10, b = 2)
## [1] 1 3 5 7 9
download.file('https://raw.githubusercontent.com/ywchiu/fubonr/master/data/trump.txt', 'trump.txt')


address <- readLines('trump.txt')
## Warning in readLines("trump.txt"): 於 'trump.txt' 找到不完整的最後一列
wordcount <- function(article){
  li <- strsplit(article, ' ')
  words <- unlist(li)
  tb <- table(words)
  res <- sort(tb,decreasing = TRUE)
  res[1:30]
}

address <- readLines('trump.txt')
## Warning in readLines("trump.txt"): 於 'trump.txt' 找到不完整的最後一列
wordcount(address)
## Warning in strsplit(article, " "): 輸入的字串 1 不適用於此語言環境
## Warning in strsplit(article, " "): 輸入的字串 12 不適用於此語言環境
## Warning in strsplit(article, " "): 輸入的字串 14 不適用於此語言環境
## Warning in strsplit(article, " "): 輸入的字串 27 不適用於此語言環境
## Warning in strsplit(article, " "): 輸入的字串 35 不適用於此語言環境
## words
##      the      and     will       of      our       to       we       is 
##       47       41       30       24       21       19       19       16 
##       We      for      all      are     have  America       be      but 
##       13       11        9        8        8        7        7        7 
##      not     your       on    their        a     been    bring       by 
##        7        7        6        6        5        5        5        5 
##    great       in     same     with   across American 
##        5        5        5        5        4        4
#install.packages('tm')
library(tm)
## Warning: package 'tm' was built under R version 3.4.2
## Loading required package: NLP
stopwords(kind='en')
##   [1] "i"          "me"         "my"         "myself"     "we"        
##   [6] "our"        "ours"       "ourselves"  "you"        "your"      
##  [11] "yours"      "yourself"   "yourselves" "he"         "him"       
##  [16] "his"        "himself"    "she"        "her"        "hers"      
##  [21] "herself"    "it"         "its"        "itself"     "they"      
##  [26] "them"       "their"      "theirs"     "themselves" "what"      
##  [31] "which"      "who"        "whom"       "this"       "that"      
##  [36] "these"      "those"      "am"         "is"         "are"       
##  [41] "was"        "were"       "be"         "been"       "being"     
##  [46] "have"       "has"        "had"        "having"     "do"        
##  [51] "does"       "did"        "doing"      "would"      "should"    
##  [56] "could"      "ought"      "i'm"        "you're"     "he's"      
##  [61] "she's"      "it's"       "we're"      "they're"    "i've"      
##  [66] "you've"     "we've"      "they've"    "i'd"        "you'd"     
##  [71] "he'd"       "she'd"      "we'd"       "they'd"     "i'll"      
##  [76] "you'll"     "he'll"      "she'll"     "we'll"      "they'll"   
##  [81] "isn't"      "aren't"     "wasn't"     "weren't"    "hasn't"    
##  [86] "haven't"    "hadn't"     "doesn't"    "don't"      "didn't"    
##  [91] "won't"      "wouldn't"   "shan't"     "shouldn't"  "can't"     
##  [96] "cannot"     "couldn't"   "mustn't"    "let's"      "that's"    
## [101] "who's"      "what's"     "here's"     "there's"    "when's"    
## [106] "where's"    "why's"      "how's"      "a"          "an"        
## [111] "the"        "and"        "but"        "if"         "or"        
## [116] "because"    "as"         "until"      "while"      "of"        
## [121] "at"         "by"         "for"        "with"       "about"     
## [126] "against"    "between"    "into"       "through"    "during"    
## [131] "before"     "after"      "above"      "below"      "to"        
## [136] "from"       "up"         "down"       "in"         "out"       
## [141] "on"         "off"        "over"       "under"      "again"     
## [146] "further"    "then"       "once"       "here"       "there"     
## [151] "when"       "where"      "why"        "how"        "all"       
## [156] "any"        "both"       "each"       "few"        "more"      
## [161] "most"       "other"      "some"       "such"       "no"        
## [166] "nor"        "not"        "only"       "own"        "same"      
## [171] "so"         "than"       "too"        "very"
#tb[! names(tb) %in% stopwords(kind='en')]

! c('a', 'b', 'c', 'd', 'e') %in% c('b', 'c', 'd')
## [1]  TRUE FALSE FALSE FALSE  TRUE
wordcount <- function(article){
  li <- strsplit(article, '[ .]')
  words <- unlist(li)
  words <- tolower(words)
  tb <- table(words)
  tb <- tb[! names(tb) %in% c(stopwords(kind='en'), 'will') ] 
  res <- sort(tb,decreasing = TRUE)
  res
}

#install.packages('wordcloud2')
library(wordcloud2)
## Warning: package 'wordcloud2' was built under R version 3.4.2
wordcloud2(wordcount(address), shape='star')
## Warning in strsplit(article, "[ .]"): 輸入的字串 1 不適用於此語言環境
## Warning in strsplit(article, "[ .]"): 輸入的字串 12 不適用於此語言環境
## Warning in strsplit(article, "[ .]"): 輸入的字串 14 不適用於此語言環境
## Warning in strsplit(article, "[ .]"): 輸入的字串 27 不適用於此語言環境
## Warning in strsplit(article, "[ .]"): 輸入的字串 35 不適用於此語言環境
?wordcloud2


download.file('https://raw.githubusercontent.com/ywchiu/fubonr/master/data/quake.txt', 'quake.txt')

article <- readLines('quake.txt')
## Warning in readLines("quake.txt"): 於 'quake.txt' 找到不完整的最後一列
wordcloud2(wordcount(article), shape='star')

迴圈函數

x <- list( c(1,2,3,4), c(5,6,7,8))
for(i in x){
  #print(i)
  print(sum(i))
}
## [1] 10
## [1] 26
lapply(x, sum)
## [[1]]
## [1] 10
## 
## [[2]]
## [1] 26
sf <- function(ele){
  sum(ele ^ 2)
}

lapply(x, sf)
## [[1]]
## [1] 30
## 
## [[2]]
## [1] 174
lapply(x, function(ele) sum(ele ^ 2) )
## [[1]]
## [1] 30
## 
## [[2]]
## [1] 174
m1 <- matrix(1:4, nrow = 2, byrow=TRUE)
m1
##      [,1] [,2]
## [1,]    1    2
## [2,]    3    4
m2 <- matrix(5:8, nrow = 2, byrow=TRUE)
m2
##      [,1] [,2]
## [1,]    5    6
## [2,]    7    8
mean(m1)
## [1] 2.5
mean(m2)
## [1] 6.5
li <- list(m1,m2)
li
## [[1]]
##      [,1] [,2]
## [1,]    1    2
## [2,]    3    4
## 
## [[2]]
##      [,1] [,2]
## [1,]    5    6
## [2,]    7    8
lapply(li,mean)
## [[1]]
## [1] 2.5
## 
## [[2]]
## [1] 6.5
getFirstRow <- function(m){
  m[1,]
}

getFirstRow(m1)
## [1] 1 2
getFirstRow(m2)
## [1] 5 6
lapply(list(m1,m2), getFirstRow)
## [[1]]
## [1] 1 2
## 
## [[2]]
## [1] 5 6
lapply(list(m1,m2), function(m) m[1,])
## [[1]]
## [1] 1 2
## 
## [[2]]
## [1] 5 6
x <- list(c(1,2,3,4), c(5,6,7,8))
lapply(x, sum)
## [[1]]
## [1] 10
## 
## [[2]]
## [1] 26
unlist(lapply(x, sum))
## [1] 10 26
sapply(x, sum)
## [1] 10 26
li <- list(m1,m2)
sapply(li, mean)
## [1] 2.5 6.5
sapply(li, function(m)m[1,])
##      [,1] [,2]
## [1,]    1    5
## [2,]    2    6
m <- matrix(1:4, nrow=2, byrow=TRUE)
m
##      [,1] [,2]
## [1,]    1    2
## [2,]    3    4
apply(m, 1, sum)
## [1] 3 7
apply(m, 2, sum)
## [1] 4 6
rowSums(m)
## [1] 3 7
colSums(m)
## [1] 4 6
apply(m, 1, mean)
## [1] 1.5 3.5
apply(m, 2, mean)
## [1] 2 3
x <- c(80,70,59,88,72,57)
t <- c(1,1,2,1,1,2)
tapply(x, t , mean)
##    1    2 
## 77.5 58.0
apartment <- rent_591_sample[rent_591_sample$building_use == '公寓/獨立套房',]

tb <- tapply(apartment$price, apartment$city, mean)
sort(tb, decreasing = TRUE)
##    台北市    新北市    金門縣    新竹縣    宜蘭縣    新竹市    台中市 
## 11685.725  8771.919  7216.667  7081.250  6988.889  6571.969  6542.906 
##    基隆市    桃園市    台東縣    高雄市    彰化縣    南投縣    嘉義市 
##  6290.625  6131.397  6000.000  5820.570  5569.697  5535.714  5491.174 
##    台南市    嘉義縣    花蓮縣    苗栗縣    屏東縣    雲林縣 
##  5483.043  5350.000  5233.333  5067.500  4691.750  4488.889
tb <- tapply(apartment$price / apartment$building_area, apartment$city, mean)
sort(tb, decreasing = TRUE)
##    台北市    新北市    基隆市    新竹市    高雄市    彰化縣    桃園市 
## 1552.7380 1202.3525  951.4991  880.5222  859.1828  851.9831  840.3967 
##    台中市    台東縣    新竹縣    南投縣    金門縣    宜蘭縣    台南市 
##  835.1679  820.0758  814.0931  799.3622  793.0556  768.0394  763.6889 
##    屏東縣    雲林縣    苗栗縣    花蓮縣    嘉義市    嘉義縣 
##  753.9136  741.4815  697.6562  654.2353  606.6729  497.7221
barplot(sort(tb, decreasing = TRUE), col='blue')