## 5.1
ages <- list(25, 35, 45)
for( age in ages ) {
print( age )
}
[1] 25 [1] 35 [1] 45
## 5.4
list_of_numbers <- list( 5, 10, 50, 200 )
for( item in list_of_numbers ) {
times_two <- item * 2
print( times_two )
}
[1] 10 [1] 20 [1] 100 [1] 400
## 5.5
for( number in 0:10 ) {
print( 7 * number )
}
[1] 0 [1] 7 [1] 14 [1] 21 [1] 28 [1] 35 [1] 42 [1] 49 [1] 56 [1] 63 [1] 70
## 5.6
list_of_numbers <- list( 25, 35, 45 )
sum_gatherer <- 0
for( number in list_of_numbers ) {
sum_gatherer <- sum_gatherer + number
}
print( sum_gatherer )
[1] 105
## 5.7
list_of_numbers <- list( 25, 35, 45 )
sum_gatherer <- 0
n <- 0
for( number in list_of_numbers ) {
sum_gatherer <- sum_gatherer + number
n <- n + 1
}
print( sum_gatherer / n )
[1] 35
## 5.8
ages <- list(
'person1' = 25,
'person2' = 35,
'person3' = 45
)
for( name in names( ages ) ) {
age <- ages[[name]]
print( paste( name, age ) )
}
[1] "person1 25" [1] "person2 35" [1] "person3 45"
## 5.10
english_to_finnish <- list(
'i' = 'minä',
'you' = 'sinä',
'social' = 'sosiaalinen',
'science' = 'tiede',
'needs' = 'tarvitsee',
'programming' = 'ohjelmointi',
'skills' = 'taidot'
)
sentence_to_translate = 'social science needs programming skills'
translated_sentence = ''
for( word in strsplit( sentence_to_translate, ' ') [[1]] ) {
translated_word <- english_to_finnish[[ word ]]
translated_sentence <- paste( translated_sentence, translated_word )
}
print( paste ( sentence_to_translate, 'in Finnish is', translated_sentence ) )
[1] "social science needs programming skills in Finnish is sosiaalinen tiede tarvitsee ohjelmointi taidot"
## 5.11
english_to_finnish <- list(
'i' = 'minä',
'you' = 'sinä',
'social' = 'sosiaalinen',
'science' = 'tiede',
'needs' = 'tarvitsee',
'programming' = 'ohjelmointi',
'skills' = 'taidot'
)
sentence_to_translate = 'i love you'
translated_sentence = ''
for( word in strsplit( sentence_to_translate, ' ') [[1]] ) {
if( word %in% names( english_to_finnish ) ) {
translated_word <- english_to_finnish[[ word ]]
translated_sentence <- paste( translated_sentence, translated_word )
} else {
translated_sentence <- paste( translated_sentence, '[unknown]' )
}
}
print( paste ( sentence_to_translate, 'in Finnish is', translated_sentence ) )
[1] "i love you in Finnish is minä [unknown] sinä"
## 5.12
degree <- list()
data <- read.csv('network.txt', sep = '-' )
for( line in 1:nrow(data) ) {
node1 <- data[line, 1]
node2 <- data[line, 2]
if( ! node1 %in% names( degree ) ) {
degree[node1] <- 0
}
if( ! node2 %in% names( degree ) ) {
degree[node2] <- 0
}
degree[node1] <- degree[[node1]] + 1
degree[node2] <- degree[[node2]] + 1
}
print( degree )
$F [1] 6 $B [1] 5 $A [1] 5 $E [1] 6 $G [1] 6 $C [1] 4 $D [1] 6
## 5.13
occurances <- list() ## this is the mega gatherer
data <- read.csv('numbers.txt', header = FALSE )
for( linenumber in 1:nrow(data) ) {
number <- as.character( data[ linenumber, 1 ] ) ## operating numbers as text clarifies this code a bit
if( ! number %in% names(occurances) ) {
occurances[number] <- 0
}
occurances[number] <- occurances[[number]] + 1
}
print( occurances )
$`1` [1] 1 $`16` [1] 1 $`4` [1] 2 $`13` [1] 1 $`5` [1] 2 $`2` [1] 1 $`18` [1] 1 $`8` [1] 1
## 5.14
connections <- list()
data <- read.csv('pairs.txt', sep = ',', header = FALSE )
for( line in 1:nrow(data) ) {
node1 <- data[line, 1]
node2 <- data[line, 2]
if( ! node1 %in% names( degree ) ) {
connections[[node1]] <- c() ## i.e., en empty list
}
connections[[node1]] <- c( connections[[node1]] , node2 )
if( ! node2 %in% names( degree ) ) {
connections[node2] <- c() ## i.e., en empty list
}
connections[[node2]] <- c( connections[[node2]] , node1 )
}
print( connections )
Warning message in read.table(file = file, header = header, sep = sep, quote = quote, : “incomplete final line found by readTableHeader on 'pairs.txt'”
$A [1] "B" "C" "D" $B [1] "A" "C" $C [1] "A" "B" $D [1] "A"
## 5.19
all_votes <- list() ## collect all votes into a list
data <- read.csv("elections.txt", header = FALSE )
for( i in 1:nrow(data) ) {
name <- data[i, 1]
party <- data[i, 2]
votes <- data[i, 3]
birth_year <- data[i, 4]
all_votes <- c( all_votes, votes )
}
print( paste( "Number of candidates", length( all_votes ) ) )
## compute mean
sum_gatherer <- 0
for( number in all_votes ) {
sum_gatherer <- sum_gatherer + number
}
print( sum_gatherer / length( all_votes) )
[1] "Number of candidates 200" [1] 5000482946
## 5.20
votes_per_party <- list() ## collect all votes into a list
data <- read.csv("elections.txt", header = FALSE )
for( i in 1:nrow(data) ) {
name <- data[i, 1]
party <- data[i, 2]
votes <- data[i, 3]
birth_year <- data[i, 4]
if( ! party %in% names( votes_per_party ) ) {
votes_per_party[[ party ]] <- 0
}
votes_per_party[[ party ]] <- votes_per_party[[ party ]] + votes
}
print( votes_per_party )
$PartyA [1] 1.00002e+12 $PartyB [1] 21572206 $PartyC [1] 23343976 $PartyD [1] 32125207
## 5.21
candidates_per_party <- list()
data <- read.csv("elections.txt", header = FALSE )
for( i in 1:nrow(data) ) {
name <- data[i, 1]
party <- data[i, 2]
votes <- data[i, 3]
birth_year <- data[i, 4]
if( ! party %in% names( candidates_per_party ) ) {
candidates_per_party[[ party ]] <- 0
}
candidates_per_party[[ party ]] <- candidates_per_party[[ party ]] + 1
}
print( candidates_per_party )
$PartyA [1] 36 $PartyB [1] 46 $PartyC [1] 52 $PartyD [1] 66
## 5.22
votes_per_party <- list() ## collect all votes into a list
data <- read.csv("elections.txt", header = FALSE )
for( i in 1:nrow(data) ) {
name <- data[i, 1]
party <- data[i, 2]
votes <- data[i, 3]
birth_year <- data[i, 4]
if( ! party %in% names( votes_per_party ) ) {
votes_per_party[[ party ]] <- c()
}
votes_per_party[[ party ]] <- c( votes_per_party[[ party ]] , votes )
}
for( party in names( votes_per_party ) ) {
votes <- votes_per_party[[party]]
votes <- sort( votes )
print( paste( party, votes[length(votes)] ) )
}
[1] "PartyA 1000000643092" [1] "PartyB 895747" [1] "PartyC 878033" [1] "PartyD 892613"
## these variables are counted in previous exercises
mean_across_parties <- sum_gatherer / length(all_votes)
elected_candidates <- 0
data <- read.csv("elections.txt", header = FALSE )
for( i in 1:nrow(data) ) {
name <- data[i, 1]
party <- data[i, 2]
votes <- data[i, 3]
birth_year <- data[i, 4]
if( votes > 2 * mean_across_parties ) {
elected_candidates = elected_candidates + 1
}
}
print( elected_candidates )
[1] 1
## these variables are counted in previous exercises
mean_across_parties <- sum_gatherer / length(all_votes)
elected_candidates_per_party <- list()
data <- read.csv("elections.txt", header = FALSE )
for( i in 1:nrow(data) ) {
name <- data[i, 1]
party <- data[i, 2]
votes <- data[i, 3]
birth_year <- data[i, 4]
if( votes > 2 * mean_across_parties ) {
if( ! party %in% names(elected_candidates_per_party) ) {
elected_candidates_per_party[[party]] <- 0
}
elected_candidates_per_party[[party]] <- elected_candidates_per_party[[party]] + 1
}
}
print( elected_candidates_per_party )
$PartyA [1] 1
## 5.25
words <- list()
unique_words <- 0
data <- readLines( file('candidates_tweets.txt') )
for( rn in 1:length(data) ) {
line <- tolower( data[[ rn ]] )
line <- gsub(".", "", data[[ rn ]] ) ## cleaning the data sligtly by removing . and ,
line <- gsub(",", "", data[[ rn ]] )
for( word in strsplit( line, " ")[[1]] ) {
if( ! word %in% words ) {
words <- c( words, word )
unique_words <- unique_words + 1
}
}
}
print( unique_words )
Warning message in readLines(file("candidates_tweets.txt")): “incomplete final line found on 'candidates_tweets.txt'”
[1] 490
## 5.26
words <- list()
data <- readLines( file('candidates_tweets.txt') )
for( rn in 1:length(data) ) {
line <- tolower( data[[ rn ]] )
line <- gsub("\\.", "", line ) ## cleaning the data sligtly by removing . and ,
line <- gsub(",", "", line )
for( word in strsplit( line, " ")[[1]] ) {
word <- as.character( word )
if( ! word %in% words ) {
words[[ word ]] <- 0
}
words[[ word ]] <- words[[ word ]] + 1
}
}
print( words )
Warning message in readLines(file("candidates_tweets.txt")): “incomplete final line found on 'candidates_tweets.txt'”
$clearly [1] 1 $has [1] 1 $a [1] 1 $tax [1] 1 $code [1] 1 $by [1] 1 $making [1] 1 $less [1] 1 $family [1] 1 $member [1] 1 $because [1] 1 $when [1] 1 $they [1] 1 $have [1] 1 $access [1] 1 $to [1] 1 $make [1] 1 $sure [1] 1 $that [1] 1 $ticket [1] 1 $is [1] 1 $`they're` [1] 1 $finally [1] 1 $happen [1] 1 $`for` [1] 1 $college [1] 1 $accreditors [1] 1 $no [1] 1 $more [1] 1 $than [1] 1 $twice [1] 1 $as [1] 1 $i [1] 1 $know [1] 1 $it [1] 1 $`before:` [1] 1 $did [1] 1 $`21st` [1] 1 $century [1] 1 $insurmountable [1] 1 $debt [1] 1 $relief [1] 1 $them [1] 1 $available [1] 1 $over [1] 1 $on [1] 1 $the [1] 1 $inflation [1] 1 $reduction [1] 1 $act [1] 1 $does [1] 1 $come [1] 1 $legacy [1] 1 $of [1] 1 $way [1] 1 $pollution [1] 1 $creating [1] 1 $thousands [1] 1 $clean [1] 1 $energy [1] 1 $economy [1] 1 $`while` [1] 1 $reducing [1] 1 $affordable [1] 1 $care [1] 1 $passes [1] 1 $allow [1] 1 $`america’s` [1] 1 $largest [1] 1 $most [1] 1 $comprehensive [1] 1 $legislation [1] 1 $rt [1] 1 $incredible [1] 1 $progress [1] 1 $`in` [1] 1 $community [1] 1 $policing [1] 1 $exposed [1] 1 $help [1] 1 $us [1] 1 $up [1] 1 $and [1] 1 $flooding [1] 1 $december [1] 1 $soul [1] 1 $same [1] 1 $folks [1] 1 $prescriptions [1] 1 $could [1] 1 $be [1] 1 $nearly [1] 1 $`$100` [1] 1 $million [1] 1 $borrowers [1] 1 $can [1] 1 $think [1] 1 $about [1] 1 $their [1] 1 $need [1] 1 $credits [1] 1 $`–` [1] 1 $especially [1] 1 $not [1] 1 $stopping [1] 1 $there [1] 1 $state [1] 1 $resume [1] 1 $federal [1] 1 $income [1] 1 $builds [1] 1 $takes [1] 1 $away [1] 1 $freedoms [1] 1 $death [1] 1 $sided [1] 1 $with [1] 1 $student [1] 1 $loan [1] 1 $payment [1] 1 $pause [1] 1 $crucial [1] 1 $investment [1] 1 $house [1] 1 $democrats [1] 1 $chose [1] 1 $families [1] 1 $breathing [1] 1 $room [1] 1 $ours [1] 1 $so [1] 1 $many [1] 1 $years [1] 1 $fight [1] 1 $`23rd` [1] 1 $ally [1] 1 $health [1] 1 $emergency [1] 1 $work [1] 1 $security [1] 1 $decades [1] 1 $at [1] 1 $industry [1] 1 $dense [1] 1 $keeping [1] 1 $america [1] 1 $put [1] 1 $our [1] 1 $history [1] 1 $take [1] 1 $track [1] 1 $ensure [1] 1 $mind [1] 1 $governor [1] 1 $ask [1] 1 $`130` [1] 1 $top [1] 1 $united [1] 1 $states [1] 1 $leads [1] 1 $workers [1] 1 $afford [1] 1 $look [1] 1 $forward [1] 1 $those [1] 1 $president [1] 1 $says [1] 1 $part [1] 1 $world [1] 1 $use [1] 1 $murder [1] 1 $victims [1] 1 $passage [1] 1 $declines [1] 1 $out [1] 1 $we [1] 1 $took [1] 1 $creation [1] 1 $its [1] 1 $people [1] 1 $supported [1] 1 $again [1] 1 $thanks [1] 1 $sign [1] 1 $two [1] 1 $bipartisan [1] 1 $bills [1] 1 $bottom [1] 1 $all [1] 1 $agree [1] 1 $event [1] 1 $tougher [1] 1 $penalties [1] 1 $opioid [1] 1 $epidemic [1] 1 $imposing [1] 1 $deadly [1] 1 $`won't` [1] 1 $just [1] 1 $fire [1] 1 $leading [1] 1 $examples [1] 1 $`@usnavy` [1] 1 $veteran [1] 1 $recorded [1] 1 $conversation [1] 1 $me [1] 1 $into [1] 1 $proud [1] 1 $future [1] 1 $great [1] 1 $personal [1] 1 $answers [1] 1 $you [1] 1 $teacher [1] 1 $are [1] 1 $building [1] 1 $stronger [1] 1 $pathways [1] 1 $tohono [1] 1 $`o’odham` [1] 1 $nation [1] 1 $wear [1] 1 $never [1] 1 $stop [1] 1 $fighting [1] 1 $humanity [1] 1 $native [1] 1 $american [1] 1 $believe [1] 1 $last [1] 1 $year [1] 1 $devastating [1] 1 $physical [1] 1 $`next` [1] 1 $generation [1] 1 $words [1] 1 $one [1] 1 $panama [1] 1 $`—` [1] 1 $your [1] 1 $`husband’s` [1] 1 $pride [1] 1 $throughout [1] 1 $support [1] 1 $`@delawareng` [1] 1 $congratulations [1] 1 $apply [1] 1 $wherever [1] 1 $okinawa [1] 1 $special [1] 1 $day [1] 1 $who [1] 1 $romania [1] 1 $opened [1] 1 $lives [1] 1 $helped [1] 1 $poverty [1] 1 $few [1] 1 $greater [1] 1 $`100` [1] 1 $awards [1] 1 $this [1] 1 $center [1] 1 $romanian [1] 1 $strengthen [1] 1 $nothing [1] 1 $like [1] 1 $months [1] 1 $far [1] 1 $too [1] 1 $ukrainians [1] 1 $after [1] 1 $`33` [1] 1 $long [1] 1 $fill [1] 1 $vacancies [1] 1 $single [1] 1 $`more!` [1] 1 $`@flotus` [1] 1 $`😻` [1] 1 $`#nationalpetday` [1] 1 $`@letourfemmes` [1] 1 $anxiety [1] 1 $become [1] 1 $parents [1] 1 $honor [1] 1 $`@nationalpta` [1] 1 $marked [1] 1 $protest [1] 1 $`florida’s` [1] 1 $`governor:` [1] 1 $how [1] 1 $much [1] 1 $tribal [1] 1 $resources [1] 1 $`require?` [1] 1 $right [1] 1 $`here!` [1] 1 $birthday [1] 1 $`you’ll` [1] 1 $always [1] 1 $infant [1] 1 $formula [1] 1 $give [1] 1 $my [1] 1 $heart [1] 1 $military [1] 1 $children [1] 1 $share [1] 1 $moment [1] 1 $stand [1] 1 $thank [1] 1 $stem [1] 1 $skills [1] 1 $person [1] 1 $hosting [1] 1 $`@secbecerra` [1] 1 $trillion [1] 1 $spending [1] 1 $scam [1] 1 $will [1] 1 $favors [1] 1 $expensive [1] 1 $`$75000` [1] 1 $per [1] 1 $ago [1] 1 $today [1] 1 $should [1] 1 $do [1] 1 $`something!` [1] 1 $sent [1] 1 $whole [1] 1 $`87000` [1] 1 $irs [1] 1 $paper [1] 1 $pushers [1] 1 $al [1] 1 $qaeda [1] 1 $border [1] 1 $past [1] 1 $already [1] 1 $suffering [1] 1 $under [1] 1 $`record-high` [1] 1 $`you're` [1] 1 $partisan [1] 1 $`things—they` [1] 1 $affect [1] 1 $everyone [1] 1 $knows [1] 1 $`what's` [1] 1 $really [1] 1 $detrimental [1] 1 $seeing [1] 1 $growth [1] 1 $`solutions-focused` [1] 1 $letter [1] 1 $cause [1] 1 $would [1] 1 $rather [1] 1 $go [1] 1 $wrong [1] 1 $doubling [1] 1 $down [1] 1 $cosponsored [1] 1 $an [1] 1 $`@repcarlos` [1] 1 $`&` [1] 1 $unaffordable [1] 1 $he [1] 1 $answer [1] 1 $`#backtoschool` [1] 1 $bakersfield [1] 1 $prevented [1] 1 $hire [1] 1 $some [1] 1 $biden [1] 1 $admin [1] 1 $must [1] 1 $gross [1] 1 $misrepresentation [1] 1 $dollars [1] 1 $job [1] 1 $creators [1] 1 $messaging [1] 1 $china [1] 1 $focus [1] 1 $benefits [1] 1 $add [1] 1 $borrow [1] 1 $`working-class` [1] 1 $`$6000` [1] 1 $post [1] 1 $`left's` [1] 1 $credit [1] 1 $restore [1] 1 $fiscal [1] 1 $uniting [1] 1 $armed [1] 1 $forces [1] 1 $gainesville [1] 1 $increase [1] 1 $remains [1] 1 $near [1] 1 $record [1] 1 $high [1] 1 $gas [1] 1 $prices [1] 1 $rising [1] 1 $crime [1] 1 $harass [1] 1 $blatantly [1] 1 $false [1] 1 $`@presssec` [1] 1 $made [1] 1 $highs [1] 1 $administration [1] 1 $conducted [1] 1 $footing [1] 1 $buying [1] 1 $main [1] 1 $street [1] 1 $lost [1] 1 $brave [1] 1 $men [1] 1 $resiliency [1] 1 $efforts [1] 1 $worsen [1] 1 $subject [1] 1 $since [1] 1 $`biden's` [1] 1 $economic [1] 1 $genius [1] 1 $call [1] 1 $anyone [1] 1 $`it's` [1] 1 $population [1] 1 $`@gopleader's` [1] 1 $commitment [1] 1 $wants [1] 1 $paycheck [1] 1 $now [1] 1 $ending [1] 1 $soon [1] 1 $`40-year` [1] 1 $cost [1] 1 $challenges [1] 1 $facing [1] 1 $country [1] 1 $what [1] 1 $was [1] 1 $disgraceful [1] 1 $disastrous [1] 1 $time [1] 1 $urging [1] 1 $him [1] 1 $veterans [1] 1 $`300%` [1] 1 $`'water` [1] 1 $during [1] 1 $fall [1] 1 $groceries [1] 1 $southern [1] 1 $deserved [1] 1 $task [1] 1 $force [1] 1 $remained [1] 1 $`president's` [1] 1 $`if` [1] 1 $migrant [1] 1 $crisis [1] 1 $trusted [1] 1 $election [1] 1 $july [1] 1 $`2020` [1] 1 $annual [1] 1 $pace [1] 1 $withdraw [1] 1
## 5.27
print( names( words ) )
## frequencies of words
frequencies <- sort( unname(unlist( words ) ) )
most_common_10 <- frequencies[[length(frequencies)-9]] ## 10th most highest frequency
for( word in names(words) ) {
frequency <- words[[word]]
print( word )
if( frequency >= most_common_10 ) {
print( paste( word, frequency ) )
}
}
[1] "==>" "candidates_tweetstxt" [3] "<==" "rt" [5] "@repmarkmeadows:" "democrats" [7] "go" "4+" [9] "weeks" "with" [11] "a" "secretive" [13] "free" "for" [15] "all" "no" [17] "rules" "impeachment" [19] "process—and" "now" [21] "try" "to" [23] "save" "face" [25] "shor…r" "@gopoversight:" [27] "days" "since" [29] "@repadamschiff" "learned" [31] "the" "identity" [33] "of" "whistleblower:" [35] "78" "https://tco/yksa5rpgpsr" [37] "@repmattgaetz:" "must-read:" [39] "\"gaetz:" "'donald" [41] "trump" "is" [43] "innocent" "and" [45] "deep" "state" [47] "guilty'\"" "https://tco/fxsdxjjls2r" [49] "@repdougcollins:" "adam" [51] "schiff" "wants" [53] "impeach" "@realdonaldtrump" [55] "not" "going" [57] "along" "“impeachment”" [59] "inquiry" "that" [61] "even" "ha…r" [63] "@repgregpence:" "after" [65] "seven" "running" [67] "communist-style" "ultra-partisan" [69] "effort" "undo" [71] "2016" "election" [73] "results" "under" [75] "disguis…r" "@repjeffduncan:" [77] "pelosi" "&" [79] "want" "pretend" [81] "they" "are" [83] "being" "transparent—after" [85] "corrupted" "tainted" [87] "process" "in" [89] "s…r" "@repgosar:" [91] "nancy" "pelosi's" [93] "resolution" "further" [95] "restricts" "participation" [97] "duly" "elected" [99] "members" "congress" [101] "from" "their" [103] "part…r" "@congressmanhice:" [105] "once" "again" [107] "@speakerpelosi" "proves" [109] "have" "intention" [111] "giving" "fair" [113] "shake" "this" [115] "\"im…r" "@jim_jordan:" [117] "dems’" "resolution:" [119] "-cuts" "oversight" [121] "foreign" "affairs" [123] "out" "public" [125] "hearings" "-pretends" [127] "give" "@devinnunes" [129] "sub…r" "bogus" [131] "attempt" "legitimize" [133] "an" "doesn’t" [135] "offer" "real" [137] "fairness" "due" [139] "process…r" [1] "==>" [1] "==> 1" [1] "candidates_tweetstxt" [1] "candidates_tweetstxt 1" [1] "<==" [1] "<== 1" [1] "rt" [1] "rt 1" [1] "@repmarkmeadows:" [1] "@repmarkmeadows: 1" [1] "democrats" [1] "democrats 1" [1] "go" [1] "go 1" [1] "4+" [1] "4+ 1" [1] "weeks" [1] "weeks 1" [1] "with" [1] "with 1" [1] "a" [1] "a 1" [1] "secretive" [1] "secretive 1" [1] "free" [1] "free 1" [1] "for" [1] "for 1" [1] "all" [1] "all 1" [1] "no" [1] "no 1" [1] "rules" [1] "rules 1" [1] "impeachment" [1] "impeachment 1" [1] "process—and" [1] "process—and 1" [1] "now" [1] "now 1" [1] "try" [1] "try 1" [1] "to" [1] "to 1" [1] "save" [1] "save 1" [1] "face" [1] "face 1" [1] "shor…r" [1] "shor…r 1" [1] "@gopoversight:" [1] "@gopoversight: 1" [1] "days" [1] "days 1" [1] "since" [1] "since 1" [1] "@repadamschiff" [1] "@repadamschiff 1" [1] "learned" [1] "learned 1" [1] "the" [1] "the 1" [1] "identity" [1] "identity 1" [1] "of" [1] "of 1" [1] "whistleblower:" [1] "whistleblower: 1" [1] "78" [1] "78 1" [1] "https://tco/yksa5rpgpsr" [1] "https://tco/yksa5rpgpsr 1" [1] "@repmattgaetz:" [1] "@repmattgaetz: 1" [1] "must-read:" [1] "must-read: 1" [1] "\"gaetz:" [1] "\"gaetz: 1" [1] "'donald" [1] "'donald 1" [1] "trump" [1] "trump 1" [1] "is" [1] "is 1" [1] "innocent" [1] "innocent 1" [1] "and" [1] "and 1" [1] "deep" [1] "deep 1" [1] "state" [1] "state 1" [1] "guilty'\"" [1] "guilty'\" 1" [1] "https://tco/fxsdxjjls2r" [1] "https://tco/fxsdxjjls2r 1" [1] "@repdougcollins:" [1] "@repdougcollins: 1" [1] "adam" [1] "adam 1" [1] "schiff" [1] "schiff 1" [1] "wants" [1] "wants 1" [1] "impeach" [1] "impeach 1" [1] "@realdonaldtrump" [1] "@realdonaldtrump 1" [1] "not" [1] "not 1" [1] "going" [1] "going 1" [1] "along" [1] "along 1" [1] "“impeachment”" [1] "“impeachment” 1" [1] "inquiry" [1] "inquiry 1" [1] "that" [1] "that 1" [1] "even" [1] "even 1" [1] "ha…r" [1] "ha…r 1" [1] "@repgregpence:" [1] "@repgregpence: 1" [1] "after" [1] "after 1" [1] "seven" [1] "seven 1" [1] "running" [1] "running 1" [1] "communist-style" [1] "communist-style 1" [1] "ultra-partisan" [1] "ultra-partisan 1" [1] "effort" [1] "effort 1" [1] "undo" [1] "undo 1" [1] "2016" [1] "2016 1" [1] "election" [1] "election 1" [1] "results" [1] "results 1" [1] "under" [1] "under 1" [1] "disguis…r" [1] "disguis…r 1" [1] "@repjeffduncan:" [1] "@repjeffduncan: 1" [1] "pelosi" [1] "pelosi 1" [1] "&" [1] "& 1" [1] "want" [1] "want 1" [1] "pretend" [1] "pretend 1" [1] "they" [1] "they 1" [1] "are" [1] "are 1" [1] "being" [1] "being 1" [1] "transparent—after" [1] "transparent—after 1" [1] "corrupted" [1] "corrupted 1" [1] "tainted" [1] "tainted 1" [1] "process" [1] "process 1" [1] "in" [1] "in 1" [1] "s…r" [1] "s…r 1" [1] "@repgosar:" [1] "@repgosar: 1" [1] "nancy" [1] "nancy 1" [1] "pelosi's" [1] "pelosi's 1" [1] "resolution" [1] "resolution 1" [1] "further" [1] "further 1" [1] "restricts" [1] "restricts 1" [1] "participation" [1] "participation 1" [1] "duly" [1] "duly 1" [1] "elected" [1] "elected 1" [1] "members" [1] "members 1" [1] "congress" [1] "congress 1" [1] "from" [1] "from 1" [1] "their" [1] "their 1" [1] "part…r" [1] "part…r 1" [1] "@congressmanhice:" [1] "@congressmanhice: 1" [1] "once" [1] "once 1" [1] "again" [1] "again 1" [1] "@speakerpelosi" [1] "@speakerpelosi 1" [1] "proves" [1] "proves 1" [1] "have" [1] "have 1" [1] "intention" [1] "intention 1" [1] "giving" [1] "giving 1" [1] "fair" [1] "fair 1" [1] "shake" [1] "shake 1" [1] "this" [1] "this 1" [1] "\"im…r" [1] "\"im…r 1" [1] "@jim_jordan:" [1] "@jim_jordan: 1" [1] "dems’" [1] "dems’ 1" [1] "resolution:" [1] "resolution: 1" [1] "-cuts" [1] "-cuts 1" [1] "oversight" [1] "oversight 1" [1] "foreign" [1] "foreign 1" [1] "affairs" [1] "affairs 1" [1] "out" [1] "out 1" [1] "public" [1] "public 1" [1] "hearings" [1] "hearings 1" [1] "-pretends" [1] "-pretends 1" [1] "give" [1] "give 1" [1] "@devinnunes" [1] "@devinnunes 1" [1] "sub…r" [1] "sub…r 1" [1] "bogus" [1] "bogus 1" [1] "attempt" [1] "attempt 1" [1] "legitimize" [1] "legitimize 1" [1] "an" [1] "an 1" [1] "doesn’t" [1] "doesn’t 1" [1] "offer" [1] "offer 1" [1] "real" [1] "real 1" [1] "fairness" [1] "fairness 1" [1] "due" [1] "due 1" [1] "process…r" [1] "process…r 1"
## 5.28
data <- readLines( file('candidates_tweets.txt') )
for( rn in 1:length(data) ) {
line <- tolower( data[[ rn ]] )
line <- gsub(".", "", data[[ rn ]] ) ## cleaning the data sligtly by removing . and ,
line <- gsub(",", "", data[[ rn ]] )
words <- list()
for( word in strsplit( line, " ")[[1]] ) {
if( ! word %in% words ) {
words[[ word ]] <- 0
}
words[[ word ]] <- words[[ word ]] + 1
}
print( words )
}
## removed ten most common
most_common_words <- list("RT", "a", "for", "to", "the", "of", "and", "that", "in", "is") ## from exercise 5.27; you could put them on list there
for( rn in 1:length(data) ) {
line <- tolower( data[[ rn ]] )
line <- gsub(".", "", data[[ rn ]] ) ## cleaning the data sligtly by removing . and ,
line <- gsub(",", "", data[[ rn ]] )
words <- list()
for( word in strsplit( line, " ")[[1]] ) {
if( ! word %in% most_common_words ) {
if( ! word %in% words ) {
words[[ word ]] <- 0
}
words[[ word ]] <- words[[ word ]] + 1
}
}
print( words )
}
## 5.29
degrees <- list()
data <- read.csv('network.txt', sep = '-' )
for( rn in 1:nrow(data) ) {
node1 <- data[ rn, 1]
if( ! node1 %in% names(degrees) ) {
degrees[[ node1 ]] <- 0
}
degrees[[ node1 ]] <- degrees[[ node1 ]] + 1
node2 <- data[ rn, 2]
if( ! node2 %in% names(degrees) ) {
degrees[[ node2 ]] <- 0
}
degrees[[ node2 ]] <- degrees[[ node2 ]] + 1
}
for( node_name in names( degrees ) ) {
degree_value <- degrees[[ node_name ]]
print ( paste( node_name , 'has a degree of:', degree_value ) )
}
## 5.30
interested_nodes <- list('A', 'B', 'C')
data <- read.csv('network.txt', sep = '-' )
for( rn in 1:nrow(data) ) {
node1 <- data[ rn, 1]
node2 <- data[ rn, 2]
if( node1 %in% interested_nodes && node2 %in% interested_nodes ) {
print( paste( node1, node2 ) )
}
}
## 5.31
edgelist <- list()
data <- read.csv('network.txt', sep = '-' )
for( rn in 1:nrow(data) ) {
node1 <- data[ rn, 1]
node2 <- data[ rn, 2]
if( ! node1 %in% names( edgelist ) ) {
edgelist[[node1]] <- list()
}
edgelist[[node1]] <- c( edgelist[[node1]], node2 )
}
print( edgelist )
## 5.32
nodes_visited <- list()
nodes_to_be_visited <- list('A')
while( length( nodes_to_be_visited ) > 0 ) {
current_node <- nodes_to_be_visited[[1]]
nodes_visited <- c( nodes_visited, current_node )
connected_nodes <- edgelist[[ current_node ]] ## from exercise 5.31
for( node in connected_nodes ) {
if( ! node %in% nodes_visited && ! node %in% nodes_to_be_visited ) {
nodes_to_be_visited <- c( nodes_to_be_visited , node )
}
nodes_to_be_visited <- nodes_to_be_visited[ nodes_to_be_visited != current_node ]
}
}
print( unlist( nodes_visited ) )