{"action":"created","issue":{"url":"https://api.github.com/repos/sebastianbarfort/sds/issues/27","labels_url":"https://api.github.com/repos/sebastianbarfort/sds/issues/27/labels{/name}","comments_url":"https://api.github.com/repos/sebastianbarfort/sds/issues/27/comments","events_url":"https://api.github.com/repos/sebastianbarfort/sds/issues/27/events","html_url":"https://github.com/sebastianbarfort/sds/issues/27","id":111483778,"number":27,"title":"Group 25: Assignment 1 ","user":{"login":"petervallebo","id":14157606,"avatar_url":"https://avatars.githubusercontent.com/u/14157606?v=3","gravatar_id":"","url":"https://api.github.com/users/petervallebo","html_url":"https://github.com/petervallebo","followers_url":"https://api.github.com/users/petervallebo/followers","following_url":"https://api.github.com/users/petervallebo/following{/other_user}","gists_url":"https://api.github.com/users/petervallebo/gists{/gist_id}","starred_url":"https://api.github.com/users/petervallebo/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/petervallebo/subscriptions","organizations_url":"https://api.github.com/users/petervallebo/orgs","repos_url":"https://api.github.com/users/petervallebo/repos","events_url":"https://api.github.com/users/petervallebo/events{/privacy}","received_events_url":"https://api.github.com/users/petervallebo/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2015-10-14T20:19:02Z","updated_at":"2015-10-25T18:23:11Z","closed_at":"2015-10-25T18:23:11Z","body":"library(dplyr)\r\nlibrary(ggplot2)\r\nlibrary(readr)\r\nlibrary(zoo)\r\nlibrary(countrycode)\r\nlibrary(stringr)\r\nlibrary(ggmap)\r\nlibrary(rvest)\r\nlibrary(gsubfn)\r\n\r\n# Read in the data\r\n\r\ndf = read_csv(\"https://raw.githubusercontent.com/MuseumofModernArt/collection/master/Artworks.csv\")\r\n\r\n# Question 1: Create a new dataframe of the stock of paintings at MOMA for each month in the year.\r\n\r\ndf$Month_acquired=as.yearmon(df$DateAcquired)\r\n\r\nstock_month = summarise( group_by(df,Month_acquired),n=n())\r\nstock_month$stock=cumsum(stock_month$n)\r\n\r\n# Question 2: Use ggplot2 and your new data frame to plot the the stock of paintings on the y-axis and the date on the x-axis.\r\n\r\n#             what kind of geom do you think is appropriate? why?\r\n#             - geom_line, since a line graph is good at visualising the movement in stocks\r\n#             color the geom you have chosen red\r\n#             add a title and custom axis labels\r\n\r\np = ggplot(stock_month, aes(x=as.numeric(Month_acquired), y=stock)) + geom_line(aes(colour='red'))\r\np + labs(x='Time', y='Number of paintings in stock',title='Number of paintings in stock over time') + theme_minimal() +theme(legend.position=\"none\")\r\n\r\n# Question 3: Create the same plot but this time the color should reflect the stock of paintings\r\n#             for curator approved and non-curator approved paintings, respectively\r\n\r\nstock_month2 = summarise( group_by(df, CuratorApproved ,Month_acquired),n=n())\r\nstock_month2 = mutate(group_by(stock_month2,CuratorApproved), stock=cumsum(n))\r\n\r\np = ggplot(stock_month2, aes(x=as.numeric(Month_acquired), y=stock, group=CuratorApproved)) + geom_line(aes(color=CuratorApproved))\r\np + labs(x='Time', y='Number of paintings in stock by curator approved or not',title='Number of paintings in stock over time') + theme_minimal()\r\n\r\n# Question 4: Create a new dataframe of the stock of paintings grouped by what department the painting belongs to.\r\n\r\nstock_month3 = summarise( group_by(df, Department ,Month_acquired),n=n())\r\nstock_month3 = mutate(group_by(stock_month3,Department), stock=cumsum(n))\r\n\r\n# Question 5: Plot this data frame using ggplot2. Which department has had the highest increase in their stock of paintings?\r\n\r\np = ggplot(stock_month3, aes(x=as.numeric(Month_acquired), y=stock, group=Department)) + geom_line(aes(color=Department))\r\np + labs(x='Time', y='Number of paintings in stock by department',title='Number of paintings in stock over time') + theme_minimal()\r\n\r\n#             Prins and illustrated books have had the largest increase in their collection.\r\n\r\n# Question 6: Write a piece of code that counts the number of paintings by each artist in the dataset.\r\n#             List the 10 painters with the highest number of paintings in MOMA's collection.\r\n\r\n\r\nartist =    df %>%\r\n  filter(!is.na(Artist),Artist!='Unknown photographer',Artist!='') %>%\r\n  group_by(Artist) %>%\r\n  summarise(n=n()) %>%\r\n  arrange(desc(n))\r\n\r\nhead(artist,n=10)\r\n\r\n# Question 7: The variable ArtistBio lists the birth place of each painter. Use this information to create a world map\r\n#             where each country is colored according to the stock of paintings in MOMA's collection.\r\n\r\n\r\n\r\n#Step 1: First we scrape adjectival names of countries from wikipedia\r\n\r\nurl <- \"https://en.wikipedia.org/wiki/List_of_adjectival_and_demonymic_forms_for_countries_and_nations\"\r\nscrape <- url %>%\r\n  html() %>%\r\n  html_nodes(xpath='//*[@id=\"mw-content-text\"]/table[1]') %>%\r\n  html_table(fill=TRUE)\r\nscrape <- scrape[[1]]\r\n\r\ncn <- select(scrape, `Country name`, Adjectivals)\r\n\r\n#Corrections to the scraped data are added\r\n\r\ncn$`Country name`[94] = \"Great Britain\"\r\ncn$`Country name`[115] = \"Ireland\"\r\ncn$`Country name`[119] = \"Ivory Coast\"\r\ncn$`Country name`[195] = \"USSR\"\r\ncn$`Country name`[251] = \"United Kingdom\"\r\ncn$`Country name`[252] = \"United States\"\r\n\r\ncn$`Country name` <- (str_split_fixed(cn$`Country name`, \",\", n=3)[,1])\r\n\r\n\r\n#Creating references\r\ncn$`Country name`[1] = \"United States\"\r\ncn$Adjectivals[1] = \"U\"\r\n\r\ncn$`Country name`[2] = \"South Africa\"\r\ncn$Adjectivals[2] = \"South\"\r\n\r\n\r\ncn$`Country name`[4] = \"New Zealand\"\r\ncn$Adjectivals[4] = \"New\"\r\n\r\ncn$`Country name`[11] = \"Yugoslavia\"\r\ncn$Adjectivals[4] = \"Yugoslav\"\r\n\r\ncn$Adjectivals[85:87] =\"nonamefrench\"\r\ncn$Adjectivals[94] = \"British\"\r\ncn$Adjectivals[129] = \"Korean\"\r\ncn$Adjectivals[168] = \"Dutch\"\r\ncn$Adjectivals[169] = \"newcaldonian\"\r\ncn$Adjectivals[209] = \"Scottish\"\r\n\r\n\r\ncn$Adjectivals[222:224] =\"nonamesouthies\"\r\ncn$Adjectivals[252] = \"American\"\r\n\r\ncn$Adjectivals <- (str_split_fixed(cn$Adjectivals, \",\", n=3)[,1])\r\ncn$Adjectivals <- (str_split_fixed(cn$Adjectivals, \" or \", n=3)[,1])\r\n\r\n#Step 2:\r\n#We extract adjectivals from the artist bio. regex could be better - but I'm not completely familiar with it yet\r\n#Furthermore a charmatch() would be usefull, it is though not supported in my version of RStudio.\r\n\r\ndf$Adjectivals <- str_extract(df$ArtistBio, \"[A-Z][a-z]*\")\r\n\r\n\r\n\r\n#-------------This could be used for having three options of matches, I just do not know how to combine them with left join------\r\n#cn$part1 <- (str_split_fixed(cn$Adjectivals, \",\", n=3)[,1])\r\n#cn$part2 <- (str_split_fixed(cn$Adjectivals, \",\", n=3)[,2])\r\n#cn$part3 <- (str_split_fixed(cn$Adjectivals, \",\", n=3)[,3])\r\n\r\n\r\n#Step 3: Preparing for maps\r\n#Combining the two, we get something that can be read in maps.\r\n\r\ndfmaps <- left_join(df, cn, by = \"Adjectivals\")\r\n\r\nmapsum= dfmaps %>%\r\n  group_by(`Country name`) %>%\r\n  summarise(n=n()) \r\n\r\nworld = map_data(\"world\")\r\n\r\nmapsum$region = mapsum$`Country name`\r\n\r\n\r\nworld2=inner_join(world,mapsum)\r\n\r\n#Step 4: Plot\r\n\r\np = ggplot(world2, aes(x = long, y = lat, group = group)) + \r\n  geom_polygon(aes(fill = world2$n)) + scale_fill_gradient(trans = \"log\") + # Can be used to see the difference, and check that each vountry is actually represented\r\n  expand_limits() + \r\n  theme_minimal()\r\nplot(p)\r\n\r\n#I think a better solution would be to have all countries plotted, as one country having 1 contribution isn't a lot different from 0.\r\n#I do not know how to do this in maps.\r\n\r\n\r\n\r\n# Question 8: The Dimensions variable lists the dimensions of each painting. Use your data manipulation skills to calculate\r\n#             the area of each painting (in cm's). Create a data frame of the five largest and five smallest paintings in \r\n#             MOMA's collection.\r\n\r\n# Make two varibles from the dimesions -> muliply to area -> sort by area -> use head and tail functions to find largest and smallest 5\r\n\r\ndf$test=substring(df$Dimensions,first=str_locate(pattern = '\"',df$Dimensions)[,1]+1)\r\ndf$test2=str_trim(str_replace_all(df$test,pattern = '[( cm  \")]', \" \"))\r\nhead(df$test2)\r\n\r\ndf$dim1=as.numeric(substring(df$test2,first=1,last=str_locate(df$test2,pattern=\"x\")[,1]-1))\r\nhead(df$dim1) # First dimension\r\n\r\ndf$dim2=as.numeric(substring(df$test2,first=str_locate(df$test2,pattern=\"x\")[,1]+1))\r\nhead(df$dim2) # Second dimenstion\r\n\r\ndf$area = df$dim1 * df$dim2 # Calculates area\r\n\r\nhead(df$area)\r\n\r\n# Dataframe with the five largest\r\n\r\nLargest = head(arrange(df,desc(area)),n=5)\r\n\r\n# Dataframe with the five smallest\r\n\r\nSmallest = head(arrange(df,area),n=5)"},"comment":{"url":"https://api.github.com/repos/sebastianbarfort/sds/issues/comments/150951563","html_url":"https://github.com/sebastianbarfort/sds/issues/27#issuecomment-150951563","issue_url":"https://api.github.com/repos/sebastianbarfort/sds/issues/27","id":150951563,"user":{"login":"sebastianbarfort","id":2847108,"avatar_url":"https://avatars.githubusercontent.com/u/2847108?v=3","gravatar_id":"","url":"https://api.github.com/users/sebastianbarfort","html_url":"https://github.com/sebastianbarfort","followers_url":"https://api.github.com/users/sebastianbarfort/followers","following_url":"https://api.github.com/users/sebastianbarfort/following{/other_user}","gists_url":"https://api.github.com/users/sebastianbarfort/gists{/gist_id}","starred_url":"https://api.github.com/users/sebastianbarfort/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sebastianbarfort/subscriptions","organizations_url":"https://api.github.com/users/sebastianbarfort/orgs","repos_url":"https://api.github.com/users/sebastianbarfort/repos","events_url":"https://api.github.com/users/sebastianbarfort/events{/privacy}","received_events_url":"https://api.github.com/users/sebastianbarfort/received_events","type":"User","site_admin":false},"created_at":"2015-10-25T18:23:11Z","updated_at":"2015-10-25T18:23:11Z","body":"Good job. \r\n\r\nYou might want to consider using the pipe oparator (`%>%`) when you're using verbs from `dplyr`. I think it makes for much more readable code. \r\n\r\nI like your approach for question 7 although I think you're going about it in a slightly inefficient way (see my solution). I think you might want to use css selectors instead of xpath. See my lecture slides. \r\n\r\nNice use of the `cumsum` function.\r\n\r\n**APPROVED**"}}