首页 \ 问答 \ 如何在R中的向量中仅删除重复值的一个实例?(How to remove only one instance of a duplicate value in a vector in R?)

如何在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
更新时间:2022-10-14 15:10

最满意答案

通常,无论谁(服务器或客户端)反序列化参数或返回值,都需要知道在运行时期望的类型(而不是什么契约)才能这样做。 在这种情况下, 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 the DataContractSerializer (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 any KnownTypeAttributes 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 ...
  • 您当然可以这样做,但我认为重要的是要知道数据传输对象(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.
  • 这是一个棘手的情况...... 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 ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)