r - Updating dataframe values with functions at scale -


I've got a data frame for which some of the conditions require manual overrides, which give different conditions Are there. However, these conditions take a consistent form (I always want to change one value with another, a specific date range, etc.).

I am thinking that there is another great way to do the following; I suspect that, seeing that I am wrapping the function call again. Here's an example:

  set.seed (1234); Library (deptter) data & lt; - data.frame (biz = sample (c ("tailo", "shipping", "technique"), 50, location = TRUE), area = sample (c ("mideast", "americas", "Asia") 50, place = TRUE), date = delegate (sic (as.Date ("2010-02-01"), length = 10, by = "1 day"), 5))  

Now, as mentioned above, I want to change the values ​​in this dataframe, subject to certain conditions regarding the values ​​of the other variables. My first idea is to use the function:

  changeFunc & lt; - Function (df, ...) {df $ region & lt; - ifelse (df $ date %% daterange & amp; $ Biz == string1, string2, as.character (data $ area)) returns (df)}  

and then dataframe To update, call that function:

  daterange & lt; - as.Date ('2010-02-05') + 0:02; String 1 & lt; - 'tailo'; String 2 & lt; - 'Southeast' data & lt; - changeFunc (data, daterange, string1, string2)  

The problem is, I want to do it again for different date ranges and again for "string 1" and "string 2" Value, eg:

  daterange & lt; - as.Date ('2010-02-07') + 0:03; String 1 & lt; - 'Shipping & Delivery'; String 2 & lt; - 'Northeast' data & lt; - ChangeFunc (data, daterange, string1, string2) Data%.% System (biz, area, date)  

Is there any other automated way to make multiple changes in the datafree values ​​under constant rules? for?

Edit: Update with a little more clarity: What I have to do at the end is not to define 'datareng', 'string 1', 'string 2' And have to call the function repeatedly. Is there a way to take advantage of the "Apply" function to create a list of predetermined values ​​for date ranges and stars, and update the dataframe at a time? Something like:

  valList & lt; - List (daterange = as.Date ('2010-02-05') + 0:02, string 1 = "tailo", string 2 = "southeast", Daterange2 = as.Date ('2010-02-07') <0, string 1_2 = "shipping", string 2_2 = "northeast")  

and apply it to the dataframe

But as you can see, I'm a bit unclear about how the function will know that string1_2 is a condition that does not match daterange2 nor daterange


Comments

Popular posts from this blog

java - Can't add JTree to JPanel of a JInternalFrame -

javascript - data.match(var) not working it seems -

javascript - How can I pause a jQuery .each() loop, while waiting for user input? -