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

如何基于列表A的行最大值获取对应列表B、C的经纬度及列索引

Solution to Match Max Yield with Corresponding Latitude & Longitude

Got it, let's tackle this problem step by step. Since your lists A, B, C are aligned (meaning A[[i]] pairs with B[[i]] and C[[i]]), we can use mapply to process each trio of dataframes together, and for each row, grab the column index of the max value in A, then pull the matching values from B and C.

Step 1: Define a helper function

First, write a function that takes three corresponding dataframes (one from each list) and returns a dataframe with the max column index, plus the matching latitude and longitude:

get_matching_coords <- function(yield_df, lat_df, lon_df) {
  # Get column index of max value for each row in yield_df
  max_col_indices <- apply(yield_df, 1, which.max)
  
  # Extract matching latitude values: for each row, take the value at max_col_indices
  matching_lats <- mapply(function(row_num, col_idx) lat_df[row_num, col_idx],
                          1:nrow(lat_df), max_col_indices)
  
  # Extract matching longitude values the same way
  matching_lons <- mapply(function(row_num, col_idx) lon_df[row_num, col_idx],
                          1:nrow(lon_df), max_col_indices)
  
  # Combine into a single result dataframe
  result_df <- data.frame(
    max_yield_col_index = max_col_indices,
    corresponding_latitude = matching_lats,
    corresponding_longitude = matching_lons
  )
  
  return(result_df)
}

Step 2: Apply the function across all list pairs

Use mapply to iterate through your three lists in parallel. Setting SIMPLIFY = FALSE ensures we get back a list of dataframes (matching the structure of your input lists):

# Process all corresponding dataframe trios
final_result_list <- mapply(get_matching_coords, A, B, C, SIMPLIFY = FALSE)

Notes to keep in mind

  • which.max only returns the first occurrence of the maximum value in a row. If your yield data has rows with multiple equal max values, you'll need to adjust the function to handle that (e.g., using which(x == max(x)) and then choosing how to pick/return multiple coordinates).
  • Make sure that for each trio of dataframes (A[[i]], B[[i]], C[[i]]), the number of rows is the same—otherwise the function will throw an error.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 08:11:02