[英]How can I count the min/max item for for a set of otherwise unique values within a dataframe in R?
I have data set in R from Billboard top hits.我有来自 Billboard 热门歌曲的 R 数据集。 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.
我是 R 的新手,所以不知道其他更简单的方法。
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?我怎样才能制作出一张能列出艺术家、歌曲和最高数字(位置)的表格,其中 1 是最高的?
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: 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.