staove7 staove7 -4 years ago 108
R Question

Extract unique value from a list of data frames into new data frame with indexing

I have a list of data frames of the following structure:

cust_num V2 V3 ...


Each data frame present a group of customers, where
cust_num
can appear more than once in a single data frame.

I want to extract the
unique
customers of each data frame and to insert them to a new data frame with
index
of the data frame (i.e., group) they came from.

Here is an example:

# df1

cust_num V2 V3 ...
1
1
2

# df2

cust_num V2 V3 ...
4
4
5


and I want my result to be:

cust_num group
1 1
2 1
4 2
5 2


I tried to use
for
loop, but I got troubles inserting the data into new data frame and create the group
index
:

for (i in 1:length(df_list)) {
x <- unique(df_list[[i]][1])
new_df <- rbind(x)
}


Thank you in advance

Answer Source

If dat is your list of data frames:

do.call(rbind,lapply(seq_along(dat), function(x) data.frame(cust_num=unique(dat[[x]][,1]),group=x))) 
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download