函数从数据子集创建新的数据帧 [英] Function to create a new dataframe from data subsets

查看:164
本文介绍了函数从数据子集创建新的数据帧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个很大的数据帧数据与一些车辆和他们的地理空间location.I能够运行一个循环来为每个车辆ID的数据子集使用以下代码。

  uniq<  -  unique(unlist(data $ vehicleid))
for(i in 1 :length(uniq)){
data_1< - subset(data,vehicleid == uniq [i])
#您想要的函数
}

我需要编写一个函数,以便我可以提取每个子集的第一行,并在一个新的单独的数据框中获取所有提取的行。如何做到这一点?

解决方案

考虑经常被忽视的可以通过一个或多个因子子集数据框,并通过函数运行子集数据框:

 #每个VECHICLE的第一行数据帧列表ID 
dfs < - by(data,data $ vehicleid,FUN = function(d),d [1,])

#ROW BIND ALL DF ELEMENTS
finaldf< ; - do.call(rbind,dfs)


I have a large data frame data with a number of vehicles and their geo spatial location.I was able to run a loop to subset the data for each vehicle id using the following code.

uniq <- unique(unlist(data$vehicleid))
for (i in 1:length(uniq)){
    data_1 <- subset(data, vehicleid == uniq[i])
    #your desired function
}

I need to write a function so that I can extract the first row of each subset and get all the extracted rows in a new separate data frame. How do I do that?

解决方案

Consider the often overlooked by which can subset dataframes by one or more factors and run subset dataframes through a function:

# 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) 

这篇关于函数从数据子集创建新的数据帧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆