使用 c() 通过持续循环值选择数据帧行

问题描述 投票:0回答:1

我有一个包含 300 多行的数据帧 (timeseries_df),并且我有循环结果值 (a1、a2、a3)。 a1、a2、a3 的值是数字,例如 a1=3、a2=56、a3=120 等等。

现在我想使用循环创建的值从数据框中选择所有行。

它应该看起来像这样:

rows <- timeseries_df[c(a1, a2, a3, a4), ]

但我需要它更加自动化,因为循环中的值的数量并不总是相同。

循环中的函数称为 near 所以当我尝试时

for (m in 1:nrow(ts_niv)){
    
    rows <- timeseries_df[c(near), ]
  }

它可以工作,但它会覆盖每个值(a1,a2)的数据帧,最后只有一个帧具有从数据帧中选择的最后一个值。因此,我需要一个数据帧,其中所有行均由循环中的值(a1、a2、a3)选择。

我认为它并没有那么复杂,但我在处理 R 方面仍然是一个初学者。也许有人可以帮助我,解决这个问题。

r dataframe for-loop row
1个回答
0
投票

假设和澄清

  1. 由于
    near()
    位于使用索引
    for
    m
    循环内,并且由于
    m
    ts_niv
    有关,我假设
    near()
    m
    ts_niv
    的函数。
  2. 由于您既没有提供
    near()
    的定义,也没有提供
    ts_niv
    中的数据样本,也没有提供
    timeseries_df
    中的数据样本,所以让我们使用以下定义进行说明:
require( purrr )        # for %>% and map

# a multi-variate time series
ts_niv <- ts( matrix( rnorm( 24 ), 8, 3 ), start = c( 1961, 1 ), frequency = 12 )

# a function that returns some row indexes from ts_niv
near <- function( index = m, data = ts_niv ){
  num_rows_to_return <- round( runif( 1, max = 6), 0 )
  round( runif( num_rows_to_return, min = 1, max = nrow( ts_niv )), 0 )
}

# another multi-variate time series
timeseries_df <- ts( matrix( rnorm( 300 ), 50, 6 ), start = c( 1961, 1 ), frequency = 12 )

获取答案

# For each row in the time-series
seq( nrow( ts_niv )) %>% 

  # Get the row-index of interest
  map( ~near( index = ., data = ts_niv )) ->

   ragged_list

索引

这是我们得到的:

  • 一个足够长的列表,可以容纳时间序列的每行一个项目
  • 列表中的每个项目都是一个整数向量,表示要从 timeseries_df
  • 获取的行索引
ragged_list
#[[1]]
#numeric(0)         <--- near() returned no indexes for this row
#
#[[2]]
#[1] 7 3 6          <--- near() returned three indexes for this row
#
#[[3]]
#[1] 6 8 3 3        <--- near() returned four indexes for this row
#
#[[4]]
#[1] 2 7 2 6 8      <--- near() returned five indexes for this row
#
#[[5]]
#[1] 4 7 3 6        <--- near() returned four indexes for this row
#
#[[6]]
#[1] 6 1 1          <--- near() returned three indexes for this row
#

这些索引代表的行

我们可以使用 timeseries_df[ j, ]timeseries_df 获取行

j

要一次获取多行,我们可以使用

timeseries_df[ a_vector_of_integers, ]

因此,让我们按照我们所做的操作来获取行索引,而不是返回这些索引,而是返回它们引用的行。

# For each row in the time-series
seq( nrow( ts_niv )) %>% 
  # Get the row-index of interest
  
  map( ~timeseries_df[ near( index = ., data = ts_niv ), ]) ->

    a_list_of_matrices

这会生成一个矩阵列表。

a_list_of_matrices
#[[1]]                      near() returned no indexes for this row
#     Series 1 Series 2 Series 3 Series 4 Series 5 Series 6
#
#[[2]]                      near() returned three indexes for this row
#     Series 1 Series 2 Series 3  Series 4 Series 5 Series 6
#[1,]   0.5295  -0.2217   0.1995  0.875480  1.48813  -0.4781
#[2,]   0.5295  -0.2217   0.1995  0.875480  1.48813  -0.4781
#[3,]  -0.6974  -0.5784  -0.1910 -0.009228 -0.09915  -0.7863
#
#[[3]]                      near() returned four indexes for this row
#     Series 1 Series 2 Series 3 Series 4 Series 5 Series 6
#[1,]   0.8119   0.6454   1.6422  -0.9117  0.07844  -1.5139
#[2,]  -1.5977   1.4027  -0.4212  -1.7027  1.79133   0.2609
#[3,]  -0.1623   1.0213  -1.1960  -1.8157  0.07729   0.4734
#[4,]  -0.1623   1.0213  -1.1960  -1.8157  0.07729   0.4734
# ...

合并答案

使用

rbind()
可以轻松组合这些:

do.call( rbind, a_list_of_matrices )
#      Series 1 Series 2 Series 3 Series 4 Series 5 Series 6
#[1,]   0.5295  -0.2217   0.1995  0.875480  1.48813  -0.4781
#[2,]   0.5295  -0.2217   0.1995  0.875480  1.48813  -0.4781
#[3,]  -0.6974  -0.5784  -0.1910 -0.009228 -0.09915  -0.7863
#[4,]   0.8119   0.6454   1.6422  -0.9117  0.07844  -1.5139
#[5,]  -1.5977   1.4027  -0.4212  -1.7027  1.79133   0.2609
#[6,]  -0.1623   1.0213  -1.1960  -1.8157  0.07729   0.4734
#[7,]  -0.1623   1.0213  -1.1960  -1.8157  0.07729   0.4734
# [8,]   0.6204  -0.8115   1.1573  0.40136  0.01218 -0.31174
# [9,]   0.8119   0.6454   1.6422 -0.91174  0.07844 -1.51386
#[10,]   0.8119   0.6454   1.6422 -0.91174  0.07844 -1.51386
#[11,]  -0.1623   1.0213  -1.1960 -1.81571  0.07729  0.47341
#[12,]  -0.1623   1.0213  -1.1960 -1.81571  0.07729  0.47341
#...
© www.soinside.com 2019 - 2024. All rights reserved.