Truncation of vector values based on another vector

Here is the general algorithm I wish to implement in R:

if (x[i]>y[i]) x[i]=y[i]

I am generally taking Census data and adjusting the dummy variables to fit my needs.

x,y are of course vectors. This problem looks like a loop is the solution. But, my loop skills in R are poor. I can seem to get my indexes to increment properly.

Thanks Cloud :)

-------------Problems Reply------------

A couple of possibilities. First with the ifelse function (since the if (){ } else{ } constuct does not work on vectors):

x <- ifelse( x > y, y, x)

Or with logical indexing:

x[ x>y ] <- y[ x>y ]

Both of these assume that x and y are the same length and are implicitly comparing and assigning elementwise so no need for an index

Good, DWin showed you already why you absolutely don't need a loop in R. But apparently you don't want to do what you asked, or your comment wouldn't make sense whatsoever.

If you want to choose which ones you want to change, you just add an extra logical vector to the solution of DWin, eg:

x <- 1:10
y <- 10:1

# say I want to change every second index
id <-

ifelse(x>y & id,y,x)

You can do what you want, as long as id :

  • is as long as x and y
  • contains 1/TRUE for can change and 0/FALSE for cannot change
Category:r Views:0 Time:2010-11-08
Tags: r

Related post

  • Create a rainbow color scale based on a vector, in the order of that vector 2011-09-14

    I'm looking for a more elegant way to do this: #Create Dataset set.seed(1) x <- runif(100) y <- runif(100) z <- y*x #Assign colors, based on z vector Data <- data.frame(Order=1:length(z),z=z) Data <- Data[order(Data$z),] Data$col <-

  • In C++, how to compute the mean of a vector of integers using a vector view and gsl_stats_mean? 2011-01-31

    my program manipulates STL vectors of integers but, from time to time, I need to calculate a few statistics on them. Therefore I use the GSL functions. To avoid copying the STL vector into a GSL vector, I create a GSL vector view, and give it to the

  • R, correlation: is there a func that converts a vector of nums to a vector of standard units 2011-04-29

    Is there a function in R that given a vector of numbers, returns another vector with the standard units corresponding to each value? where... standard unit: how many SDs a value is + or - from the mean Example: x <- c(1,3,4,5,7) # note: mean=4, sd

  • Vectors of pointers to other vector's elements 2011-05-15

    EI have function which takes as parameter pointer to vector: void Function(std::vector<type>* aa) Now inside this function I want to filter out data from that vector to another vector and I want to change data of original vector by changing val

  • Create vector of sizes of the vectors inside a "2d-vector" (vector< vector >)? 2011-08-11

    I have a vector of vectors, say std::vector< std::vector<int> > my2dArray; Now I would like to have a vector that contains the sizes of the vectors inside my2dArray. Manually this would look something like std::vector<int> mySizes;

  • cast stl::vector containing pointers to stl::vector containing constant pointers 2011-09-14

    Consider the following class: class SomeInfo { private: std::vector<someObj *> _myVector; public: const std::vector<const someObj*>& getInfoVector() const { return _myVector; } }; When I try to compile with gcc 4.1.2, it gives me the

  • Actionscript 3 Vector Number does not extend Vector * 2011-11-16

    I am currently being confused by the Vector class. I wrote a beautiful XML to TypedClass parser. Works beautifully and without fault. UNTIL a co-worker noticed we got a Conversion Error for Vector.<Number> to Vector.<*>. Every Vector I've

  • calling an interrupt vector inside an another interrupt vector 2011-12-21

    Can I call an interrupt vector inside an another interrupt vector? For example, while I'm doing a procedure in ep4inout interrupt vector: void ep4inout interrupt 0 { . . . Timer1 = 1; //call interrupt for timer 1 . . . } Edit My code looks roughly li

  • How to create all of the vectors that are some sample vector missing one element? 2012-02-09

    I have some vector of elements passed to a function and I want to create all of the vectors that are that original vector, but with one element missing. What is the simplest way to do this in C++? My current approach is something of the following, bu

  • std::vector< std::vector > or std::deque< std::vector >? 2012-02-15

    I have an existing algorithm and I need to optimize it sligtly if it is possible. Changing a lot in this algorithm is not an option at the moment. The algoritm works with instance of std::vector< std::vector<unsigned char> >. It looks lik

  • Push_back 1D Vector as Row into 2D Vector Array 2012-03-01

    I'm trying to define the values of a 'row' or 1D vector and then push_back that row into a 2D vector. I've tried a couple different things that don't throw errors but also don't seem to work. Code below: #include <vector> #include <iostream

  • Erase element in vector while iterating the same vector 2012-03-29

    Possible Duplicate: Erasing from a std::vector while doing a for each? I'm trying to implement vertice coloring according to this algorithm; /* Given G=(V,E): Compute Degree(v) for all v in V. Set uncolored = V sorted in decreasing order of Degree(v)

  • Rotation of a pointcloud based on normal vector 2011-06-21

    I'm looking to rotate a 3-D point cloud based on the orientation of the floor. I have detected the floor and calculated its normal vector. I want to use this normal vector to ensure that all points on the floor have the same y-value. Since the dot pr

  • Vector creation and modification based on other vectors 2011-11-22

    I would like to make the code more efficient. The example creates a vector (called 'new_vector'). The values of this 'new_vector' are changed based on if/else-conditions that refer to the values of three other vectors of the same length. If the condi

  • Remove vector elements based on the index 2011-12-08

    I wanted to remove the elements of the vector based on the index, say all the even indexed elements. I have read about the erase remove idiom but can't see how to apply it. This is what I tried: vector<int> line; line.reserve(10); for(int i=0;i

  • How can I create a triangular matrix based on a vector, in MATLAB? 2009-06-16

    Let's say I've got a vector like this one: A = [101:105] Which is really: [ 101, 102, 103, 104, 105 ] And I'd like to use only vector/matrix functions and operators to produces the matrix: 101 102 103 104 105 102 103 104 105 0 103 104 105 0 0 104 105

  • replace values in a vector based on another vector 2010-10-11

    I would like to replace values in a vector (x) with values from another vector (y). Catch 22: The methods needs to be dynamic to accommodate different number of "levels" in vector x. For instance, consider vector x x <- sample(c(1, 2, 3, 4, 5), 10

  • CUDA kernel's vectors' length based on threadIdx 2011-04-14

    This is part of the pseudo code I am implementing in CUDA as part of an image reconstruction algorithm: for each xbin(0->detectorXDim/2-1): for each ybin(0->detectorYDim-1): rayInit=(xbin*xBinSize+0.5,ybin*xBinSize+0.5,-detectordistance) rayEnd

  • Which are vector graphics based image format supported by iphone? 2011-07-09

    I have developed one game for iphone which uses png images.It works well but which are the vector graphics images supported by iphone that i can use for my game for better quality of image like when we zoom the image the image displayed properly.whic

Copyright (C), All Rights Reserved.

processed in 0.202 (s). 11 q(s)