将循环输出存储在R中的每个迭代的数据帧中

将循环输出存储在R中的每个迭代的数据帧中,r,R,我想做的是存储veh.velocity的输出,并将其组合到每个迭代的新数据帧y中。我知道最好先设置一个空数据框,然后在最后合并数据列。不同的迭代也有不同数量的行。仅仅考虑前20个是可能的吗?非常抱歉,如果下面有一些问题和误解,我几个月前才开始编程。谢谢 CSV文件: 若你们只想绘制一个单独车辆ID的时间序列,我不确定是否有理由使用循环。例如,您可以轻松创建时间变量并使用ggplot2进行绘图: 您可以使用tidyr中的pivot_来重塑数据: library(tidyr) result <

我想做的是存储veh.velocity的输出,并将其组合到每个迭代的新数据帧y中。我知道最好先设置一个空数据框,然后在最后合并数据列。不同的迭代也有不同数量的行。仅仅考虑前20个是可能的吗?非常抱歉,如果下面有一些问题和误解,我几个月前才开始编程。谢谢

CSV文件:


若你们只想绘制一个单独车辆ID的时间序列,我不确定是否有理由使用循环。例如,您可以轻松创建时间变量并使用ggplot2进行绘图:

您可以使用tidyr中的pivot_来重塑数据:

library(tidyr)
result <- df1 %>% 
            group_by(id) %>%
            mutate(time = 1:n()) %>%
            dplyr::select(time, xVelocity) %>% 
            pivot_wider(id_cols = time, values_from = xVelocity,
                        names_prefix = "Veh.", names_from = id)
result
# A tibble: 414 x 287
    time Veh.1 Veh.3 Veh.7 Veh.11 Veh.12 Veh.14 Veh.15 Veh.25 Veh.31 Veh.47 Veh.50 Veh.53 Veh.55 Veh.59
   <int> <dbl> <dbl> <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>
 1     1  40.8  35.7  32.6   24.7   35.8   41.5   35.7   37.9   39.2   39.2   40.3   39.5   33.0   38.2
 2     2  40.9  35.7  32.6   24.8   35.8   41.5   35.7   37.9   39.2   39.2   40.3   39.5   33.0   38.2
 3     3  40.9  35.7  32.6   24.8   35.8   41.5   35.7   38.0   39.2   39.2   40.3   39.6   33.0   38.2
 4     4  40.9  35.7  32.6   24.8   35.8   41.5   35.7   38.0   39.2   39.2   40.3   39.6   33.1   38.2
 5     5  40.9  35.7  32.6   24.8   35.8   41.5   35.7   38.0   39.2   39.2   40.3   39.6   33.1   38.1
 6     6  40.9  35.7  32.6   24.9   35.8   41.6   35.7   38     39.2   39.2   40.4   39.6   33.1   38.1
 7     7  40.9  35.7  32.7   24.9   35.8   41.6   35.7   38.0   39.2   39.2   40.4   39.6   33.1   38.1
 8     8  40.9  35.7  32.7   24.9   35.8   41.6   35.7   38.0   39.2   39.3   40.4   39.6   33.1   38.1
 9     9  41.0  35.8  32.7   25.0   35.8   41.6   35.7   38.0   39.2   39.3   40.4   39.6   33.1   38.1
10    10  41.0  35.8  32.7   25     35.8   41.6   35.7   38.1   39.2   39.3   40.4   39.7   33.1   38.1
# … with 404 more rows, and 272 more variables:

尝试访问您的csv文件时出错。您是否已授予正确的访问权限?总的来说,如果在R中使用for循环,有一种更好的方法。当然也有例外,但我怀疑这里没有。我的首选方法是制作数据,然后使用group_by处理每辆车。如果做不到这一点,我将使用lappy来处理每个车辆,然后绑定_行以加入结果。另外,考虑从结果演示中分离数据准备。我不认为在一个循环中进行绘图是个好主意,对于大多数情况来说,这将是太多的无用情节。如果您想对数据进行一些转换,在大多数情况下,“变异”和“转化”将是更好的选择。很少会出现创建空数据的情况。需要创建空数据帧。此外,在某些情况下,purrr::map_dfr可能非常有用。
library(dplyr)
library(ggplot2)
df1 %>% 
  group_by(id) %>%
  mutate(time = 1:n()) %>%
ggplot(aes(x = time, y = xVelocity, color = as.factor(id))) +
  geom_line(show.legend = FALSE, alpha = 0.5)
library(tidyr)
result <- df1 %>% 
            group_by(id) %>%
            mutate(time = 1:n()) %>%
            dplyr::select(time, xVelocity) %>% 
            pivot_wider(id_cols = time, values_from = xVelocity,
                        names_prefix = "Veh.", names_from = id)
result
# A tibble: 414 x 287
    time Veh.1 Veh.3 Veh.7 Veh.11 Veh.12 Veh.14 Veh.15 Veh.25 Veh.31 Veh.47 Veh.50 Veh.53 Veh.55 Veh.59
   <int> <dbl> <dbl> <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>
 1     1  40.8  35.7  32.6   24.7   35.8   41.5   35.7   37.9   39.2   39.2   40.3   39.5   33.0   38.2
 2     2  40.9  35.7  32.6   24.8   35.8   41.5   35.7   37.9   39.2   39.2   40.3   39.5   33.0   38.2
 3     3  40.9  35.7  32.6   24.8   35.8   41.5   35.7   38.0   39.2   39.2   40.3   39.6   33.0   38.2
 4     4  40.9  35.7  32.6   24.8   35.8   41.5   35.7   38.0   39.2   39.2   40.3   39.6   33.1   38.2
 5     5  40.9  35.7  32.6   24.8   35.8   41.5   35.7   38.0   39.2   39.2   40.3   39.6   33.1   38.1
 6     6  40.9  35.7  32.6   24.9   35.8   41.6   35.7   38     39.2   39.2   40.4   39.6   33.1   38.1
 7     7  40.9  35.7  32.7   24.9   35.8   41.6   35.7   38.0   39.2   39.2   40.4   39.6   33.1   38.1
 8     8  40.9  35.7  32.7   24.9   35.8   41.6   35.7   38.0   39.2   39.3   40.4   39.6   33.1   38.1
 9     9  41.0  35.8  32.7   25.0   35.8   41.6   35.7   38.0   39.2   39.3   40.4   39.6   33.1   38.1
10    10  41.0  35.8  32.7   25     35.8   41.6   35.7   38.1   39.2   39.3   40.4   39.7   33.1   38.1
# … with 404 more rows, and 272 more variables: