如何在R中的向量中仅删除重复值的一个实例?(How to remove only one instance of a duplicate value in a vector in R?)
让我们考虑数值“x”的向量。 某些值可能重复。 我需要逐个删除最大值,直到x为空。
问题,如果我使用:
x <- x[x != max(x)]
它删除所有重复数等于最大值。 我想只删除其中一个重复项。 所以到现在为止,我这样做:
max.x <- x[x == max(x)] max.x <- max.x[1:length(max.x) - 1] x <- c(x[x != max(x)], max.x)
但这远非计算效率,而且我在R上找不到合适的方法也不够好。 有人可以有更好的伎俩吗?
谢谢
Let's consider a vector of numeric values "x". Some values may be duplicates. I need to remove the max value one by one until x is empty.
Problem, if I use:
x <- x[x != max(x)]
It removes all duplicates equal to the maximum. I want to remove only one of the duplicates. So until now, I do:
max.x <- x[x == max(x)] max.x <- max.x[1:length(max.x) - 1] x <- c(x[x != max(x)], max.x)
But this is far from computationally efficient, and I'm not good enough at R to find the right way to do this. Can someone has a better trick?
Thanks
原文:https://stackoverflow.com/questions/8102541
最满意答案
通常,无论谁(服务器或客户端)反序列化参数或返回值,都需要知道在运行时期望的类型(而不是什么契约)才能这样做。 在这种情况下,
object
类型的声明参数不会为客户端提供足够的信息来反序列化服务器可能发送回的任何内容。 使用DataContractSerializer
支持的类型替换它(并使用[KnownType]
修饰,如果服务器计划在回调中发送子类实例)解决了这个问题。你也可以使用配置告诉DataContractSerializer有关已知的类型,但这是应用程序范围的,对我来说似乎是一个钝器。
在我的合同看起来像这样的情况下,我有一个模糊的案例:
[ServiceContract] interface ISearch { [OperationContract] SearchResult Search(SearchQuery query); } [DataContract] [KnownType(typeof(Subclass1InSharedAssembly)), etc...] class SearchResult { [DataMember] BaseClassDeclaredInSharedAssembly Value { get; set; } }
因为我希望服务器和客户端使用(有用的)共享程序集类型,所以我在两者中都引用了共享程序集,并选择不为它们生成任何代理类型...所以
svcutil
没有生成任何KnownTypeAttribute
为了我。 部分课程保存了我的培根; 在客户端上 ,您可以这样做:// generated proxy .cs [DataContract] partial class SearchResult { ... } // hand-written .cs [KnownType(typeof(Subclass1InSharedAssembly)), etc.] partial class SearchResult { // Now the client knows how to deserialise derived types. }
In general, whoever (server or client) is deserialising a parameter or return value needs to know what types (rather, what contracts) to expect at runtime in order to do so. In this case, a declared parameter of type
object
doesn't give the client enough information to deserialise whatever the server might be sending back to it. Replacing it with a type supported by theDataContractSerializer
(and decorated with[KnownType]
, if the server plans to send subclass instances in the callback) solves this problem.You can also use configuration to tell the DataContractSerializer about known types, but this is application-wide and seems like a bit of a blunt instrument to me.
I had an obscure case once where my contracts looked something like this:
[ServiceContract] interface ISearch { [OperationContract] SearchResult Search(SearchQuery query); } [DataContract] [KnownType(typeof(Subclass1InSharedAssembly)), etc...] class SearchResult { [DataMember] BaseClassDeclaredInSharedAssembly Value { get; set; } }
Because I wanted the server and client to use the (useful) shared assembly types, I had a reference to the shared assembly in both and chose not to generate any proxy types for them... and so
svcutil
didn't generate anyKnownTypeAttribute
s for me. Partial classes saved my bacon; On the client, you can do this:// generated proxy .cs [DataContract] partial class SearchResult { ... } // hand-written .cs [KnownType(typeof(Subclass1InSharedAssembly)), etc.] partial class SearchResult { // Now the client knows how to deserialise derived types. }
相关问答
更多-
编辑: 最近得到了这个赞赏,我感到不得不说,我不会再这样做。 $.ajax返回一个promise以便您可以直接使用承诺以更加一致和可靠的方式完成我刚才所做的工作。 function customRequest(u,d) { var promise = $.ajax({ type: 'post', data: d, url: u }) .done(function (responseData, status, xhr) { // preconfig ...
-
WCF自定义类参数(WCF Custom Class Arguments)[2022-04-30]
您当然可以这样做,但我认为重要的是要知道数据传输对象(DTO)为什么不通过服务引用公开逻辑。 用于生成访问WCF服务的客户端代理的WSDL \ XSD元数据仅通过公开的操作和交换的数据类型来描述Web服务。 具体来说,XSD只描述DTO的结构而不是逻辑 - 这就是客户端代理上只有默认构造函数和公共属性/字段的原因。 因此,解决方案是将客户端和服务之间的所有自定义类交换到一个单独的共享库中。 这样,线路的两端都可以访问通过WSDL \ XSD无法获得的附加逻辑(如参数化构造函数)。 You can certa ... -
这是警告级别消息,所以您应该可以使用错误处理程序,例如: set_error_handler(function() { /* this will be executed on error */ }); imap_open(); restore_error_handler(); 这里是类似的问题: https : //stackoverflow.com/a/1241751/3470670 在这里你可以找到关于设置自定义错误处理程序的完整文档http://php.net/manual/en/function. ...
-
通常,无论谁(服务器或客户端)反序列化参数或返回值,都需要知道在运行时期望的类型(而不是什么契约)才能这样做。 在这种情况下, object类型的声明参数不会为客户端提供足够的信息来反序列化服务器可能发送回的任何内容。 使用DataContractSerializer支持的类型替换它(并使用[KnownType]修饰,如果服务器计划在回调中发送子类实例)解决了这个问题。 你也可以使用配置告诉DataContractSerializer有关已知的类型,但这是应用程序范围的,对我来说似乎是一个钝器。 在我的合同 ...
-
尽管模型回调的顺序,当前的设计使模块和类非常耦合。 要解决当前问题并改进设计,您可以在模块的方法中定义预期的回调,然后包含此模块的类可以自由地响应它。 module MyAwesomeCustomModule extend ActiveSupport::Concern included do after_save: wipe_preferences_changed end def wipe_preferences_changed # previous logic to w ...
-
您可以使用OkHttp Interceptor来监视和修改api调用。 You can use an OkHttp Interceptor to monitor and modify api calls.
-
在Guardjs上使用Google Auth时,从未调用自定义回调(Custom Callback never called when Google Auth on passportjs)[2023-06-05]
这是一个棘手的情况...... passport.authenticate方法,返回一个函数。 如果你以这种方式使用,你必须自己调用它。 看: router.get('/callback', function (req, res) { console.log("GOOGLE CALLBACK"); passport.authenticate('google', function (err, profile, info) { console.log("PROFILE: ", profile); ... -
获取qsort的现有实现并更新它以引用Sorter对象的局部变量。 它不会调用传入的比较函数,而是更新其状态并返回调用者。 由于qsort中的递归,您需要在Sorter对象中保留某种状态堆栈。 您可以使用动态分配(效率较低)使用数组或链表完成此操作。 由于大多数qsort实现对较大的一半使用尾递归,并对枢轴点的较小一半进行qsort的递归调用,因此如果数组可以容纳n个状态,则可以排序至少2 n个元素。 Take an existing implementation of qsort and update i ...
-
您可以使用以下内容创建函数指针: var yourCallback = function() { alert('2'); } 您可以将其视为包含函数本身的对象(而不是其返回值)。 由于它是一个对象,您可以将其传递给其他函数。 然后将它传递给您作为参数调用的函数(不要使用()调用它,因为它将传递其返回值): function1(params, yourCallback); 最后,在function1中,在需要时调用回调: function function1(args, callback) { ...
-
您必须从CMB2的回调函数返回一个关联数组,以生成自定义字段。 以下是如何从自定义帖子类型返回帖子下拉列表的示例: $cmb->add_field( [ 'name' => __( 'Posts dropdown', 'cmb2' ), 'id' => $prefix . 'dropdown', 'type' => 'select', 'show_option_none' => true, 'optio ...