0

I have a data frame "MYdata" and want to split this up to create three new datasets "test_1", "test_2" and "test_3". The first one "test_1" should contain only the first column from "MYdata", the second dataset should only contain the second column and so on. I am trying this:

for (i in 1:3)
{
test_[i] <- MYdata[i]
}

But I get the following error message: "Error in test_[i] <- MYdata[i] : object 'test_' not found"

Any ideas?

  • `[` is for indexing an existing object - not for creating new object names. BTW: you should learn using lists (but then eventually there is no need to construct new objects because a dataframe is a special kind of list). – jogo Aug 02 '18 at 13:23

1 Answers1

0

You can use assign to include a value in an object with name specified by paste0.

for (i in 1:3) {
  assign(paste0("test_", i), MYdata[i])
}

Each column will be a new data frame.

Bruno Pinheiro
  • 964
  • 8
  • 20