我有一个大型数据框data
,其中包含多个车辆及其地理空间位置。我可以使用以下代码运行循环来为每个车辆ID分配数据。
uniq <- unique(unlist(data$vehicleid))
for (i in 1:length(uniq)){
data_1 <- subset(data, vehicleid == uniq[i])
#your desired function
}
我需要编写一个函数,以便我可以提取每个子集的第一行,并在一个新的独立数据框中获取所有提取的行。我该怎么做?
答案 0 :(得分:1)
考虑经常被忽视的by
,它可以通过一个或多个因子对数据帧进行子集化,并通过函数运行子集数据帧:
# LIST OF FIRST ROW DATA FRAMES FOR EACH VECHICLE ID
dfs <- by(data, data$vehicleid, FUN=function(d), d[1,])
# ROW BIND ALL DF ELEMENTS
finaldf <- do.call(rbind, dfs)
答案 1 :(得分:0)
以下是提取第一行4个ID的示例
const Discord = require('discord.js');
var bot = new Discord.Client();
const PREFIX = "+";
var fortunes = [
"Yes",
"No",
"Maybe",
" a wild swear has appeared"
];
bot.on("message", message => {
bot.on("message", function (message) {
if (message.author.equals(bot.user)) return;
if (!message.content.startsWith(PREFIX)) return;
var args = message.content.substring(PREFIX.length).split(" ");
switch (args[0].toLowerCase()) {
case "ping":
message.channel.send("Pong!")
break;
case "info":
message.channel.send("no")
break;
case "8ball":
if (args[1]) message.channel.sendMessage(fortunes[Math.floor(Math.random() * fortunes.length)]);
else message.channel.send("Cant read that")
break;
case "embed":
var embed = new Discord.RichEmbed()
.addField("Test Title", "test description");
message.channel.sendEmbed(embed);
break;
case "noticeme":
console.log('Received #' + message.id + ': ' + message.content);
message.channel.send(message.author.toString() + " Senpai has noticed you")
.then(message => console.log('Sent #' + message.id + ': ' + message.content))
.catch(console.error);
break;
}
});
});
bot.login("NOPE")
替代:
example <- expand.grid(id=letters[1:4], value=5:10)
ids <- unique(example$id)
plyr::ldply(ids, function(x) example[example$id==x,][1,])
# id value
# 1 a 5
# 2 b 5
# 3 c 5
# 4 d 5