Function to create a new dataframe from data subse

2019-08-18 16:16发布

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?

2条回答
Fickle 薄情
2楼-- · 2019-08-18 16:23

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) 
查看更多
爷的心禁止访问
3楼-- · 2019-08-18 16:44

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,))
查看更多
登录 后发表回答