Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Debug the R-code below and check that each of the three statistics works after you debug it. hot

Debug the R-code below and check that each of the three statistics works after you debug it.

hot <- read.csv("../datasets/Hot_100.csv")

get_unique_songs <- function( dat ){ return( unique( dat$song_id ) ) }

weeks_on_chart <- function( dat ){ unique_songs <- get_unique_songs( dat ) weeks_on_chart <- rep( NA, length(unique_songs) ) for(j in 1:length( unique_songs ) ){ ii <- which( dat$song_id == unique_songs ) weeks_on_chart[i] <- max( dat$weeks_on_chart[ii] ) } ord <- order( weeks_on_chart, decreasing = TRUE ) return( data.frame( song = unique_songs[ord], weeks_on_chart = weeks_on_chart[ord] ) ) }

weeks_in_top10 <- function( dat ){ unique_songs <- get_unique_songs( dat ) weeks_in_top10 <- rep( NA, length( unique_songs ) ) for(j in 1:length( unique_songs ) ){ ii <- which( dat$song_id == unique_songs ) weeks_in_top10[i] <- sum( dat$week_position[ii] <= 10 ) } ord <- order( weeks_in_top10, decreasing = TRUE ) return( data.frame( song = unique_songs[ord], weeks_in_top10 = weeks_in_top10[ord] ) ) }

weeks_at_num1 <- function( dat ){ unique_songs <- get_unique_songs( dat ) weeks_at_num1 <- rep( NA, length( unique_songs ) ) for(j in 1:length( unique_songs ) ){ ii <- which( dat$song_id == unique_songs ) weeks_at_num1[i] <- sum( dat$week_position[ii] <= 10 ) } ord <- order( weeks_in_top10, decreasing = TRUE ) return( data.frame( song = unique_songs[ord], weeks_at_num1 = weeks_in_top10 ) ) }

rank_songs <- function( dat, stat = "weeks on chart" ){ if( stat == "weeks on chart" ){ rank_db <- weeks_on_chart( dat ) } else if( stat == "weeks in top 10" ){ rank_db <- weeks_in_top10( dat ) } else if( stat == "weeks at num 1" ){ rank_db <- weeks_at_num1( dat ) } else { stop("unrecognize statistic") } return( rank_db ) }

r <- rank_songs( hot, "weeks on chart" )

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

real world application of directed graph

Answered: 1 week ago