使用for循环创建数据表

时间:2016-12-29 03:13:57

标签: r for-loop data.table

假设我有data.table

DT=data.table(name = sample(letters[1:25],30,replace=T), value =rnorm(3000)) 

我希望按名称拆分DT,并创建与名称值对应的名称DT_aDT_b等新DT。那我怎么能这样做呢?

2 个答案:

答案 0 :(得分:0)

试试这个:

DTs = split(DT, DT$name)
lapply(names(DTs), function(x) assign(paste0('DT_',x), DTs[[x]], envir=.GlobalEnv))

答案 1 :(得分:0)

我们可以使用void SQLiteDatabase::GetTableProperties(DatabaseTable *table, std::vector<std::wstring> &errorMsg) { sqlite3_stmt *stmt = NULL; std::wstring errorMessage; int result; std::wstring query = L"SELECT * FROM \"sys.abcattbl\" WHERE \"abt_tnam\" = ? AND \"abt_ownr\" = ?;"; const unsigned char *dataFontName, *headingFontName, *labelFontName; int res = sqlite3_prepare_v2( m_db, sqlite_pimpl->m_myconv.to_bytes( query.c_str() ).c_str(), (int) query.length(), &stmt, 0 ); if( res == SQLITE_OK ) { const char *name = sqlite_pimpl->m_myconv.to_bytes( table->GetTableName().c_str() ).c_str(); // I used this line for debugging purposes res = sqlite3_bind_text( stmt, 1, sqlite_pimpl->m_myconv.to_bytes( table->GetTableName().c_str() ).c_str(), -1, SQLITE_STATIC ); if( res == SQLITE_OK ) { res = sqlite3_bind_text( stmt, 2, sqlite_pimpl->m_myconv.to_bytes( table->GetSchemaName().c_str() ).c_str(), -1, SQLITE_STATIC ); if( res == SQLITE_OK ) { while( true ) {

list2env

但是,建议将其保留在lst <-split(DT, DT$name) list2env(setNames(lst, paste0("DT_", names(lst))), envir = .GlobalEnv) 中,而不是在全局环境中创建多个数据集。但是,如果这仅仅是为了理解创建对象的可能性,则可以使用list