I have data set in R from Billboard top hits. I am able to count the number of unique hits for a given artist (see code below) but having trouble figuring out how to find the highest point on the charts a song went. The only thing I can think of is that before I filter out the unique values to run a loop for each song and calculate the min value. I am new to R so unaware of other easier ways.
mydata=read.csv("Hot100.csv")
mydata <- mydata[order(mydata$artist, mydata$song, mydata$date),]
head(mydata)
# date position song artist
# 218482 2000-07-01 40 Bye Bye Bye 'N Sync
# 226912 2002-02-09 70 Girlfriend 'N Sync
# 226997 2002-02-16 55 Girlfriend 'N Sync
# 227072 2002-02-23 30 Girlfriend 'N Sync
# 227164 2002-03-02 22 Girlfriend 'N Sync
# 227260 2002-03-09 18 Girlfriend 'N Sync
# to remove some cols - leaves artist and song. Has duplicates
mysub = subset(mydata, select = -c(date, position))
# now to make unique
mysub_u = unique(mysub[,c(1,2)])
View(mysub_u)
# put into table form
mytable = table(mysub_u$artist)
# but this is table form , not df
df=as.data.frame(mytable)
head(df)
# Var1 Freq
# 1 'N Sync 7
# 2 'N Sync & Gloria Estefan 1
# 3 'N Sync Featuring Nelly 1
# 4 'Til Tuesday 1
# 5 "Weird Al" Yankovic 2
# 6 (+44) 1
How could I great a table which would list the artist, song and highest number (position) it went to, with 1 being the highest?
It would have been nice to have a bigger dataset (or given any usable data) to play with. However, here is a way to do demonstrated on the small data you provided.
library(readr)
library(dplyr)
mydata <- read_table2("index date position song artist
218482 2000-07-01 40 Bye_Bye_Bye 'N_Sync
226912 2002-02-09 70 Girlfriend 'N_Sync
226997 2002-02-16 55 Girlfriend 'N_Sync
227072 2002-02-23 30 Girlfriend 'N_Sync
227164 2002-03-02 22 Girlfriend 'N_Sync
227260 2002-03-09 18 Girlfriend 'N_Sync")
out <- mydata %>%
group_by(artist,song) %>%
mutate(highest_position = min(position)) %>%
select(-index,-date,-position) %>%
unique(.)
Output:
> out
# A tibble: 2 x 3
# Groups: artist, song [2]
song artist highest_position
<chr> <chr> <dbl>
1 Bye_Bye_Bye 'N_Sync 40
2 Girlfriend 'N_Sync 18
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.