简体   繁体   English

如何在 R 中将两个相同长度的向量合并为一个长度相同的向量

[英]How do I merge two vectors of same length into one vector that has the same length as well in R

I have two vectors like this:我有两个这样的向量:

vec1<-c(0, 0, 1, 1, 0, 0, 0, 0, 0, 0)
vec2<-c(0, 0, 0, 1, 0, 0, 1, 0, 1, 0)

I want to merge it somehow to turn it into this:我想以某种方式合并它以将其变成这样:

vec<-c(0, 0, 1, 1, 0, 0, 1, 0, 1, 0)

Is there any way to do this?有没有办法做到这一点?

We can use pmax我们可以使用pmax

pmax(vec1, vec2)
[1] 0 0 1 1 0 0 1 0 1 0

or with |或与|

+(vec1|vec2)

Here is a solution with ifelse :这是ifelse的解决方案:

ifelse(vec1==0 & vec2==0, 0, 1)

[1] 0 0 1 1 0 0 1 0 1 0

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在R中比较两个相同长度的向量,但不是逐个对象地比较 - How to compare two vectors of the same length in R, but not object by object 在 R 中使向量长度相同 - Make vectors same length in R R中相同长度向量的函数 - Function for same length of vectors in R 如何在R中组合两个不同长度的向量 - How do I combine two vectors of different length in R 如何通过将相同的值写入同一行来将两个不同长度的向量写入一个数据帧? - How to write two vectors of different length into one data frame by writing same values into same row? R:从长度为 n 的向量中抽取 2 个长度为 n 的随机非重叠样本(对于相同的索引) - R: take 2 random non-overlapping samples (for same indexes) of length n out of vector of length n as well 如何合并矢量列表(在R中),使它们具有最小长度? - How can I merge a list of vectors (in R) such that they have a minimum length? 如何解决:输入必须是任意长度的字符向量或字符向量列表,每个字符向量的长度为1 - How do I solve : Input must be a character vector of any length or a list of character vectors, each of which has a length of 1 我如何解决以下错误?输入必须是任意长度的字符向量或字符向量列表,每个字符向量的长度为1 - How do I solve the following error?Input must be a character vector of any length or a list of character vectors, each of which has a length of 1 在R中,如何查看字符向量的三个元素在长度为5的向量中是否相同 - in R, how can I see if three elements of a character vector are the same out of a vector of length 5
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM