r 按新索引对行进行排序

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了r 按新索引对行进行排序相关的知识,希望对你有一定的参考价值。

# data frame of UTM and long/lat coords
# with zone and bearing
# ordered by increasing bearing within each zone
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
c_coords_df <- c_coords_longlat %>% 
  map_df(
    ~ as.data.frame(.x),
    .id = "zone"
  ) %>% 
  
  rename(
    x_long = X,
    y_lat = Y
  ) %>% 
  
  mutate(
    index = row_number()
  ) %>% 
  
  # add bearing values
  left_join(c_bearings_df) %>% 
  
  # add UTM coords
  left_join(c_coords_UTM_df) %>% 
  
  # sort by increasing bearing within each zone
  group_by(zone) %>%
  
  mutate(
    zone_rows = n(),
    zone_index = row_number(),
    bearing_min = which.min(bearing),
    index_bearing = case_when(
      zone_index < bearing_min ~ zone_rows - bearing_min + zone_index + 1,
      zone_index >= bearing_min ~ zone_index - bearing_min + 1)
  ) %>% 
  
  ungroup %>% 

  arrange(zone, index_bearing) %>% 
  
  # re-index
  mutate(
    index = row_number()
  ) %>% 
  
  select(index, zone, bearing, everything(), -c(zone_rows, zone_index, bearing_min))
  
  # tmp <- bind_rows(l_c_coords[[2]])
# tmp <- tmp %>%
#   mutate(index = row_number(),
#          which_x = first(which(x == UTM_coords$X)),
#          index_x = case_when(
#            index <=  which_x ~ which_x - row_number() + 1,
#            index >   which_x ~ nrow(.) - row_number() + which_x + 1)) %>%
#   arrange(index_x) 

# tmp <- bind_rows(l_c_coords[[2]])
# tmp <- tmp %>%
#   mutate(index = row_number(),
#          which_x = first(which(x == UTM_coords$X)),
#          index_x = case_when(
#            index <  which_x ~ index + nrow(.) - which_x + 1,
#            index >= which_x ~ row_number() - which_x + 1)) %>%
#   arrange(index_x)
# 
# ###
# 
# tmp <- bind_rows(l_c_coords[[2]])
# tmp <- tmp %>%
#   mutate(index = row_number(),
#          index_y = case_when(
#            index <  which.max(y) ~ index + nrow(.) - which.max(y) + 1,
#            index >= which.max(y) ~ row_number() - which.max(y) + 1 )) %>%
#   arrange(index_y)

以上是关于r 按新索引对行进行排序的主要内容,如果未能解决你的问题,请参考以下文章

如何在新视图中按新索引分组

Linux:cut、sort都是针对列操作,有没有对行操作的命令?

按自定义数组对行进行排序

NetSuite - 对行项目进行排序

AngularJS按表头对行进行排序

Bootstrap jquery sortable 只对行进行排序?