简体   繁体   中英

Looping through dataset in R

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM