library(languageR)

Baayen 2008 Page 20 The data set spanishMeta contains metadata about fifteen texts sampled from three Spanish authors. Each line in this file provides information on a single text. Later in this book we will consider whether these authors can be distinguished on the basis of the quantitative characteristics of their personal styles (gauged by the relative frequencies of function words and tag trigrams). Exercise 1 Display this data frame in the R terminal. Extract the column names from the data frame. Also extract the number of rows.

spanishMeta #Display this data frame in the R terminal.
##    Author YearOfBirth  TextName PubDate Nwords    FullName
## 1       C        1916 X14458gll    1983   2972        Cela
## 2       C        1916 X14459gll    1951   3040        Cela
## 3       C        1916 X14460gll    1956   3066        Cela
## 4       C        1916 X14461gll    1948   3044        Cela
## 5       C        1916 X14462gll    1942   3053        Cela
## 6       M        1943 X14463gll    1986   3013     Mendoza
## 7       M        1943 X14464gll    1992   3049     Mendoza
## 8       M        1943 X14465gll    1989   3042     Mendoza
## 9       M        1943 X14466gll    1982   3039     Mendoza
## 10      M        1943 X14467gll    2002   3045     Mendoza
## 11      V        1936 X14472gll    1965   3037 VargasLLosa
## 12      V        1936 X14473gll    1963   3067 VargasLLosa
## 13      V        1936 X14474gll    1977   3020 VargasLLosa
## 14      V        1936 X14475gll    1987   3016 VargasLLosa
## 15      V        1936 X14476gll    1981   3054 VargasLLosa
colnames(spanishMeta)  #Extract the column names from the data frame.
## [1] "Author"      "YearOfBirth" "TextName"    "PubDate"     "Nwords"     
## [6] "FullName"
nrow(spanishMeta) #Also extract the number of rows.
## [1] 15

Exercise 2 Calculate how many different texts are available in meta for each author. Also calculate the mean publication date of the texts sampled for each author.

AuthC=subset(spanishMeta,Author=="C")
AuthM=subset(spanishMeta,Author=="M")
AuthV=subset(spanishMeta,Author=="V")

nrow(AuthC) #Number of Texts available for Cela
## [1] 5
nrow(AuthM) #Number of Texts available for Mendoza
## [1] 5
nrow(AuthV) #Number of Texts available for VargasLLosa
## [1] 5
mean(AuthC[,4]) #Mean publication date for Cela
## [1] 1956
mean(AuthM[,4]) #Mean publication date for Mendoza
## [1] 1990.2
mean(AuthV[,4]) #Mean publication date for VargasLLosa
## [1] 1974.6

Exercise 3 Sort the rows in meta by year of birth (YearOfBirth) and the number of words sampled from the texts (Nwords).

spanishMeta[order(spanishMeta$YearOfBirth, spanishMeta$Nwords),] #Sort the rows in meta by year of birth (YearOfBirth) and the number of words sampled from the texts (Nwords).
##    Author YearOfBirth  TextName PubDate Nwords    FullName
## 1       C        1916 X14458gll    1983   2972        Cela
## 2       C        1916 X14459gll    1951   3040        Cela
## 4       C        1916 X14461gll    1948   3044        Cela
## 5       C        1916 X14462gll    1942   3053        Cela
## 3       C        1916 X14460gll    1956   3066        Cela
## 14      V        1936 X14475gll    1987   3016 VargasLLosa
## 13      V        1936 X14474gll    1977   3020 VargasLLosa
## 11      V        1936 X14472gll    1965   3037 VargasLLosa
## 15      V        1936 X14476gll    1981   3054 VargasLLosa
## 12      V        1936 X14473gll    1963   3067 VargasLLosa
## 6       M        1943 X14463gll    1986   3013     Mendoza
## 9       M        1943 X14466gll    1982   3039     Mendoza
## 8       M        1943 X14465gll    1989   3042     Mendoza
## 10      M        1943 X14467gll    2002   3045     Mendoza
## 7       M        1943 X14464gll    1992   3049     Mendoza

Exercise 4 Extract the vector of publication dates from meta. Sort this vector. Consult the help page for sort() and sort the vector in reverse numerical order. Also sort the row names of meta.

spanishMeta[,4] #Extract the vector of publication dates from meta.
##  [1] 1983 1951 1956 1948 1942 1986 1992 1989 1982 2002 1965 1963 1977 1987
## [15] 1981
PubYear=spanishMeta[,4]
sort(PubYear) #Sort this vector.
##  [1] 1942 1948 1951 1956 1963 1965 1977 1981 1982 1983 1986 1987 1989 1992
## [15] 2002
sort(PubYear, decreasing=TRUE) #Consult the help page for sort() and sort the vector in reverse numerical order.
##  [1] 2002 1992 1989 1987 1986 1983 1982 1981 1977 1965 1963 1956 1951 1948
## [15] 1942
RowNames=order(spanishMeta)
sort(RowNames) #Also sort the row names of meta.
##  [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
## [24] 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
## [47] 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
## [70] 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90

Exercise 5 Extract from meta all rows with texts that were published before 1980.

subset(spanishMeta,PubDate<1980) #Extract from meta all rows with texts that were published before 1980.
##    Author YearOfBirth  TextName PubDate Nwords    FullName
## 2       C        1916 X14459gll    1951   3040        Cela
## 3       C        1916 X14460gll    1956   3066        Cela
## 4       C        1916 X14461gll    1948   3044        Cela
## 5       C        1916 X14462gll    1942   3053        Cela
## 11      V        1936 X14472gll    1965   3037 VargasLLosa
## 12      V        1936 X14473gll    1963   3067 VargasLLosa
## 13      V        1936 X14474gll    1977   3020 VargasLLosa

Exercise 6 Calculate the mean publication date for all texts. The arithmetic mean is defined as the sum of the observations in a vector divided by the number of elements in the vector. The length of a vector is provided by the function length(). Recalculate the mean year of publication by means of the functions sum() and length().

mean(spanishMeta[,4]) #Calculate the mean publication date for all texts.
## [1] 1973.6
sum(spanishMeta[,4])/length(spanishMeta[,4]) #Recalculate the mean year of publication by means of the functions sum() and length().
## [1] 1973.6

Exercise 7 We create a new data frame with fictitious information on each author’s favorite composer with the function data.frame(). Add the information in this new data frame to meta with merge().

composer = data.frame(Author = c("Cela","Mendoza","VargasLLosa"), Favorite = c("Stravinsky", "Bach", "Villa-Lobos"))
merge(spanishMeta,composer, by.x="FullName", by.y="Author") #Add the information in this new data frame to meta with merge().
##       FullName Author YearOfBirth  TextName PubDate Nwords    Favorite
## 1         Cela      C        1916 X14458gll    1983   2972  Stravinsky
## 2         Cela      C        1916 X14459gll    1951   3040  Stravinsky
## 3         Cela      C        1916 X14460gll    1956   3066  Stravinsky
## 4         Cela      C        1916 X14461gll    1948   3044  Stravinsky
## 5         Cela      C        1916 X14462gll    1942   3053  Stravinsky
## 6      Mendoza      M        1943 X14463gll    1986   3013        Bach
## 7      Mendoza      M        1943 X14464gll    1992   3049        Bach
## 8      Mendoza      M        1943 X14465gll    1989   3042        Bach
## 9      Mendoza      M        1943 X14466gll    1982   3039        Bach
## 10     Mendoza      M        1943 X14467gll    2002   3045        Bach
## 11 VargasLLosa      V        1936 X14472gll    1965   3037 Villa-Lobos
## 12 VargasLLosa      V        1936 X14473gll    1963   3067 Villa-Lobos
## 13 VargasLLosa      V        1936 X14474gll    1977   3020 Villa-Lobos
## 14 VargasLLosa      V        1936 X14475gll    1987   3016 Villa-Lobos
## 15 VargasLLosa      V        1936 X14476gll    1981   3054 Villa-Lobos