You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求助:为Spotify榜单数据获取函数输出添加日期列

Hey there, let's work through this Spotify chart data issue you're dealing with! It sounds like you're pulling Top 200 tracks over a date range, but the final track list isn't linked to their respective chart dates—and when you try to add a date column, the length mismatch between your input dates and output tracks is blocking you. Let's break this down and fix it.

First, let's diagnose the root cause

From your code snippet, you've created a date sequence datums spanning April 1 to May 5, 2018. The most likely issues are:

  • Your data-fetching function is only pulling tracks for a single date (hence the 200 rows) instead of looping through all dates in datums.
  • You aren't attaching the corresponding date to each day's track list before merging all data together.

Solution 1: Fetch and attach dates in one go (cleanest approach)

Using purrr::map_dfr (from the tidyverse) is a great way to loop through your dates, pull each day's chart, and automatically merge everything into a single data frame with a date column. Here's how to adjust your code (I'll assume you're using the spotifyr package since it's common for this use case—tweak the API call to match your actual function):

library(tidyverse)
library(spotifyr)

# Your original date sequence
datums <- seq(as.Date('2018-04-01'), as.Date('2018-05-05'), by = 1)

# Define a helper function to pull one day's chart + add the date column
get_daily_top200 <- function(chart_date) {
  # Pull the chart for the given date (adjust region/chart type as needed)
  daily_chart <- get_spotify_chart(region = "US", date = chart_date, chart = "top200")
  # Attach the date to every row in this day's chart
  daily_chart <- daily_chart %>% mutate(chart_date = chart_date)
  return(daily_chart)
}

# Loop through all dates and combine results into one data frame
all_charts <- map_dfr(datums, get_daily_top200)

This will give you a data frame where every track row has its corresponding chart_date—no length mismatches here, since each date's 200 tracks get tagged before merging.

Solution 2: Fix existing incomplete data

If you already have that 200-row dataset and need to retroactively add dates (though this only makes sense if those 200 tracks are from a single date), you can just add a column with the specific date they correspond to:

# Replace "2018-XX-XX" with the actual date these tracks are from
your_existing_data <- your_existing_data %>% mutate(chart_date = as.Date("2018-XX-XX"))

But if you meant to pull data for all dates in datums and only got 200 rows, double-check your original loop logic. For example, if you used a for loop without appending data, you might have overwritten the data frame each time instead of merging:

# ❌ Wrong way (overwrites data each iteration)
all_charts <- data.frame()
for (date in datums) {
  all_charts <- get_spotify_chart(date = date)
}

# ✅ Correct way (appends each day's data)
all_charts <- data.frame()
for (date in datums) {
  daily_chart <- get_spotify_chart(date = date)
  daily_chart$chart_date <- date
  all_charts <- rbind(all_charts, daily_chart)
}

Quick debugging check

If you're still confused about the length mismatch, run these lines to verify counts:

# How many dates are you trying to pull?
cat("Total dates in sequence:", length(datums), "\n")

# How many tracks does one day's chart return?
test_chart <- get_spotify_chart(date = datums[1])
cat("Tracks per day:", nrow(test_chart), "\n")

# Expected total rows: length(datums) * nrow(test_chart)
cat("Expected total tracks:", length(datums) * nrow(test_chart), "\n")

If your actual total tracks don't match the expected number, some dates might have failed to pull. Add error handling with purrr::possibly to skip problematic dates:

# Make the helper function fail gracefully
safe_get_chart <- possibly(get_daily_top200, otherwise = NULL)
all_charts <- map_dfr(datums, safe_get_chart)

内容的提问来源于stack exchange,提问作者BJSP

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 03:37:18