Function to create a new dataframe from data subsets

久未见 提交于 2019-12-25 11:53:22

问题


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?


回答1:


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) 



回答2:


Here's an example of extracting first row of 4 ids

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

Alternative:

example_list <- split(example, example$id)
do.call(rbind, lapply(example_list, '[', 1,))


来源:https://stackoverflow.com/questions/47025787/function-to-create-a-new-dataframe-from-data-subsets

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!