0

I have the following code:

DATAFRAMEINPUT <- read.csv2("R:/2018_01_F2.csv", header=TRUE)
DATAFRAMEINPUT <- read.csv2("R:/2018_02_F2.csv", header=TRUE)
DATAFRAMEINPUT <- read.csv2("R:/2018_03_F2.csv", header=TRUE)

Instead of 2018_01 I want n-36, i.e., the 36th previous month.

Instead of 2018_02 I want n-35, i.e., the 35th previous month.

Instead of 2018_03 I want n-34, i.e., the 34th previous month.

I want to define a base month on the code and then count backwards 36 months.

I want to create a monthly routine program.

Can you help me writing the proper code?

ramgouveia
  • 5
  • 1
  • 4
  • Your question is not clear, can you please include sample input and expected output? We don't have access to your files, but if it's just about filenames, then the question can be reduced to a discussion about `character` and `Date` objects. – r2evans Apr 16 '21 at 14:51
  • @r2evans see answer below please. – ramgouveia Apr 16 '21 at 16:50

3 Answers3

0

I'll assume zero-padded. Showing just 3 here,

sprintf("R:/MON_%02i_F2.csv", 1:3)
# [1] "R:/MON_01_F2.csv" "R:/MON_02_F2.csv" "R:/MON_03_F2.csv"
allframes <- lapply(sprintf("R:/MON_%02i_F2.csv", 1:3), read.csv2)

If you instead need "R:/MON_2_F2.csv" (not zero-padded), then change the format-string to "R:/MON_%i_F2.csv".

From there, please see https://stackoverflow.com/a/24376207/3358227 for discussion on working with a list-of-frames.

r2evans
  • 141,215
  • 6
  • 77
  • 149
0

I have run this code and solved the problem.

# Defining Variables
MESANALISE <- "01_2021"
# MESINICIAL - Substituir na linha 16 o primeiro dia desse mês
FLUXO <- "F1"
FLUXO2 <- "FL1"

# Creates a vector with the name of the 36 files to import
DATAFRAMEINPUTFILES <- seq(from = as.Date("2018/01/01"),
                           by = "month",
                           length.out = 36)
DATAFRAMEINPUTFILES <- substr(DATAFRAMEINPUTFILES, 1, 7)
DATAFRAMEINPUTFILES <- paste(substr(DATAFRAMEINPUTFILES, 6, 7), substr(DATAFRAMEINPUTFILES, 1, 4), sep = "_")
DATAFRAMEINPUTFILES <- paste0("R:/", FLUXO2, "/", DATAFRAMEINPUTFILES, "_", FLUXO, ".csv")

# Import the files
for(i in 1:length(DATAFRAMEINPUTFILES)) {                             
  assign(paste0("DATAFRAMEINPUT_COM_TT_N_", i),                                  
         read.csv2(DATAFRAMEINPUTFILES[i]))
}
ramgouveia
  • 5
  • 1
  • 4
-1

r2evans

I want the code to look like this

DATAFRAMEINPUT <- read.csv2("R:/MON_36_F2.csv", header=TRUE)
DATAFRAMEINPUT <- read.csv2("R:/MON_35_F2.csv", header=TRUE)
DATAFRAMEINPUT <- read.csv2("R:/MON_34_F2.csv", header=TRUE)

And add a command line where I define MON

ramgouveia
  • 5
  • 1
  • 4
  • This is not an answer, this is clarification that should go into the original question. Please [edit](https://stackoverflow.com/posts/67127440/edit) the original question, add this text there, then delete this answer. – r2evans Apr 16 '21 at 17:25