通过R中的循环将数据子集到不同的DataFrame中

时间:2017-11-07 20:26:35

标签: r loops dataframe subset

我正在寻找循环中几个数据帧的子集变量,将数据帧保存为子集之前的相同名称。名称遵循以下结构:rx.2003,rx.2004,rx.2005等。

我很难找到一个解决方案,以便在循环中正确地将数据集中。下面的代码产生以下错误:

//Initiallising node modules var express = require('express'); var app = express(); var bodyParser = require('body-parser'); var sql = require("mssql"); var form = require('multer'); // Body Parser Middleware app.use(bodyParser.json()); app.use(bodyParser.urlencoded({ extended: false })); var urlencodedParser = bodyParser.urlencoded({ extended: false }); app.use(bodyParser.text({ type: 'text/html' })); //CORS Middleware app.use(function (req, res, next) { //Enabling CORS res.header("Access-Control-Allow-Origin", "*"); res.header("Access-Control-Allow-Methods", "GET,HEAD,OPTIONS,POST,PUT"); res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, contentType,Content-Type, Accept, Authorization"); next(); }); //Setting up server var server = app.listen(process.env.PORT || 8080, function () { var port = server.address().port; console.log("App now running on port", port); }); // Configure database connection var dbConfig = { user: 'user', domain: 'domain', password: 'password', server: 'server', database: 'database' }; //Function to connect to database and execute query var executeQuery = function(res, query){ sql.connect(dbConfig, function (err) { if (err) { console.log("Error while connecting database :- " + err); res.send(err); } else { // create Request object var request = new sql.Request(); // query to the database request.query(query, function (err, rs) { if (err) { console.log("Error while querying database :- " + err); res.send(err); } else { res.send(rs); sql.close(); } }); } }); }; //GET API app.get("/api/user", function(req , res){ var query = "select * from [Names]"; executeQuery (res, query); }); // //POST API app.post('/api/user', function (req, res) { res.setHeader('Content-Type', 'text/plain'); // connect to your database sql.connect(dbConfig, function (err) { if (err) console.log(err); // create Request object var request = new sql.Request(); request.input('first_name',sql.VarChar,req.body.first_name); request.input('last_name',sql.VarChar,req.body.last_name); request.input('gender',sql.VarChar,req.body.gender); request.query("INSERT INTO Names (First_Name, Last_Name, Gender) VALUES (@first_name, @last_name, @gender)", function (err, recordset) { if (err) console.log(err); // send records as a response res.send(recordset); sql.close(); }); }); }); // //POST API // app.post("/api/user", function(req , res){ // //var query = "INSERT INTO Names (First_Name, Last_Name, Gender) VALUES ('Test', 'User', 'Female')"; // var query = "INSERT INTO Names (First_Name,Last_Name,Gender) VALUES ()"; // executeQuery (res, query); // }); //PUT API app.put("/api/user/:id", function(req , res){ var query = "UPDATE [Names] SET First_Name= " + req.body.first_name + " , Last_Name= " + req.body.last_name + " , Gender= " + req.body.gender + " WHERE Id= " + req.params.id; executeQuery (res, query); }); // DELETE API app.delete("/api/user/:id", function(req , res){ var query = "DELETE FROM [Names] WHERE Id=" + req.params.id; executeQuery (res, query); });

Error in ... target of assignment expands to non-language object

我知道这个错误表明R认为我要求它将文本" rx.2003"的子集,但我想弄清楚如何指示R到子集数据帧本身。

建议?

1 个答案:

答案 0 :(得分:0)

也许你可以试试

 for(i in 2003:2015)
 { 
  DF <- get(paste0("rx.",i))
  assign(paste0("rx.",i),subset(DF,select = c("ID", "Drug", "Year")
 }

希望这有帮助