删除嵌套列表数据框中的重复行 [英] Remove duplicate rows in nested list data frame

查看:111
本文介绍了删除嵌套列表数据框中的重复行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有嵌套列表的数据框:

I have a data frame with a nested list:

df <- structure(list(zerobonds = c(1, 1, NA), nominal = c(20, 20, NA
), calls = list(list(c(NA, -1), 1), list(list(NA, -1), 1), NA), 
call_strike = list(list(c(NA, 90), 110), list(list(NA, 90), 
                                              110), NA), puts = list(NA, NA, list(c(NA, 1), -1)), put_strike = list(
                                                NA, NA, list(c(NA, 110), 90))), row.names = c(NA, -3L
                                                ), class = "data.frame")
df
##   zerobonds nominal     calls call_strike      puts  put_strike
## 1         1      20 NA, -1, 1 NA, 90, 110        NA          NA
## 2         1      20 NA, -1, 1 NA, 90, 110        NA          NA
## 3        NA      NA        NA          NA NA, 1, -1 NA, 110, 90

我的问题:您看到第一行和第二行重复.我想删除此类数据框中的所有重复行,并且正在寻找一些通用方法.

My question: You see that the first and second row are duplicated. I want to remove all duplicate rows in such data frames and I am looking for some general method.

我尝试过的操作:duplicated似乎不起作用,我猜是因为内部嵌套列表的数据框的这种特殊结构.

What I tried: duplicated doesn't seem to work, I guess because of this special structure of a data frame with nested lists inside.

推荐答案

您可能需要展平每列的嵌套列表,然后应用unique,例如

You may need to flatten the nested lists of each column and then apply unique, e.g.,

> unique({df[]<-Map(function(x) Map(unlist,x),df);df})
  zerobonds nominal     calls call_strike      puts  put_strike
1         1      20 NA, -1, 1 NA, 90, 110        NA          NA
3        NA      NA        NA          NA NA, 1, -1 NA, 110, 90

这篇关于删除嵌套列表数据框中的重复行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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