0

I'm trying to figure out how can I add something to a data frame df, based on a variable (i.e. a date), ending up with a data frame named df_17 if variable is equal to 2017 for example.

The reason why I want this is because I'm importing datasets from several years and quarters, and I would like to make sure that they are named according to the year variable they have. Each dataset only has 1 date. I know I can do it manually but it would take me less time to automate it.

I know how to do it with columns and rows, but I can't figure it out for objects.

EDIT:

Example 1: Data frame name "df"

A   B   Date
1   4   2017
2   3   2017

New data frame name "df_2017"

Example 2: Data frame name "df"

A   B   Date
1   4   2016
2   3   2016

New data frame name - "df_2016 "

FilipeTeixeira
  • 1,100
  • 2
  • 9
  • 29
  • Depends on the object type, the value type and so forth. Please provide us with a minimal reproducible example to illustrate what you're after: http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Joris Meys Mar 31 '17 at 09:23
  • 4
    Do not create independent objects. I repeat, do not do this. Put these objects into a list. – Roland Mar 31 '17 at 09:26
  • @JorisMeys I've added an example. Hope it's clearer now. – FilipeTeixeira Mar 31 '17 at 09:29

1 Answers1

0

The assign function should do what you want. A solution could look like

assign(paste0("df_", year), dataframe_read_from_file, pos = 1)

If you use assign inside a function oder a loop, make sure that you set the pos option correctly.

Wolfgang
  • 574
  • 4
  • 11
  • 3
    I knew as soon as I saw the question someone would write an answer promoting bad practice. -1 – Roland Mar 31 '17 at 09:27
  • This is not necesarily a bad practice... There are several circumstances where putting dataframes in a `list` is not useful – Wolfgang Mar 31 '17 at 09:29
  • @Roland, why is this a bad practice? Just curious as it actually seems to solve my question. – FilipeTeixeira Mar 31 '17 at 09:37
  • I agree, that in this case a list would be a better solution, because the data is similar, but if you have several data files that are not similar (e.g. different tables of a data base) then it makes no sense to store it in a list. – Wolfgang Mar 31 '17 at 09:40
  • @Wolfgang But if they are not similar there is also no reason to name them programmatically. – Roland Mar 31 '17 at 12:05
  • @FilipeTeixeira Your next question will be how to work with these objects in a loop. And the answer will be they should not be independent objects. Put them into a list. – Roland Mar 31 '17 at 12:06
  • @Roland probably I'm missing something here, but as my dataset has 30 variables and 9.000.000 observations, and as I'm using it already for network analysis in igraph, I'm not sure what's the advantage of having a list. But again, I'm quite new on this so I might be missing something. I've managed already to do all merging and data cleaning, and the posterior network analysis. I only had this issue for a feature I'm adding. – FilipeTeixeira Mar 31 '17 at 17:52