Home > Mobile >  How can we store these dates into another dataframe as we loop through?
How can we store these dates into another dataframe as we loop through?

Time:09-16

In R (studio), I have tried so many iterations of storing just the dates into the turning_point_dates data frame, but I have only been able to get it to store the loop numbers. I can print out each date as it is found, but not able to store them yet.

dates = data.frame(Date = seq(from = as.Date("2002-06-01"), to = as.Date("2011-09-30"), by = 'day'))
   
nums = c(98,99,100,101,102,103,104,105,106,107)
dataframe_of_numbers = data.frame(nums)    

mat = matrix(ncol=0, nrow=0)
turning_point_dates = data.frame(mat)

for (i in 1:nrow(dataframe_of_numbers)){
   print(dates$Date[dataframe_of_numbers[i,]])
   turning_point_dates[i,] = dates$Date[dataframe_of_numbers[i,]]
}

turning_point_dates

How can I instead store the actual dates that are being looped over into the turning_point_dates data frame?

turning_point_dates puts out a data frame looking like the following: Description:df [10 x 0]

1
2
3
4
5
6
7
8
9
10
1-10 of 10 rows

When I want instead a data frame like so:

"2002-09-06" "2002-09-07" "2002-09-08" "2002-09-09" "2002-09-10" "2002-09-11" "2002-09-12" "2002-09-13" "2002-09-14" "2002-09-15"

CodePudding user response:

It's a bit unclear, but if you want to end up with a smaller dataframe that only has the dates corresponding to the row numbers in nums, you don't need to use a loop. You can just subset the data frame with num, as shown below.

I'm also suggesting using a tibble instead of a basic data.frame, because subsetting a tibble returns a tibble but subsetting a data.frame returns a vector.

library(tibble)

dates <- tibble::tibble(Date = seq(from = as.Date("2002-06-01"), 
                                   to = as.Date("2011-09-30"), 
                                   by = 'day'))

nums <- c(98,99,100,101,102,103,104,105,106,107)

dates_subsetted <- dates[nums,]

It can also be done with a loop, but in my view it's much clunkier. It will almost certainly be much, much slower if you have a lot of data.

But since it was asked:

library(dplyr)
# set up another tibble for the values we'll extract
dates_looped <- tibble::tibble()

# loop through each row of the input, add that row if 
for (i in 1:nrow(dates)){
  if (i %in% nums) {
    dates_looped <- dplyr::bind_rows(dates_looped, dates[i,])
  }  
}

dates_looped and dates_subsetted are the same, but making dates_subsetted took a single line of code that will run many times faster.

CodePudding user response:

I don't think you need a loop to do this. Here is what I did:

dates <- data.frame(Date = seq(from = as.Date("2002-06-01"), to = as.Date("2011-09-30"), by = 'day'))

nums = c(98,99,100,101,102,103,104,105,106,107)

turning_point_dates <- data.frame(nums, dates = dates$Date[nums])
  • Related