[R] Simple Model in R
Rui Barradas
ruipbarradas at sapo.pt
Sat Sep 7 13:27:16 CEST 2013
Hello,
What you can do is to write a function to do the change. When you want
to change a value in column Value, it will update the value in column
New. Something like this:
fun <- function(data, row, newval){
data$Value[row] <- newval
data$New <- c(NA, diff(data$Value))
data
}
fun(dat, 2, 101.5)
Hope this helps,
Rui Barradas
Em 07-09-2013 12:19, Ankur Seth escreveu:
> Is there a way in which I can setup a model like that?
>
> Regards,
> Ankur Seth
>
>
> On Sat, Sep 7, 2013 at 4:49 PM, Rui Barradas <ruipbarradas at sapo.pt> wrote:
>
>> Hello,
>>
>> It will not change the value automatically, you will have to rerun the
>> code.
>>
>> Rui Barradas
>>
>> Em 07-09-2013 11:52, Ankur Seth escreveu:
>>
>>> Thanks Rui, but this does not change the value in the new column
>>> automatically if I change the value in the data column. Any ideas?
>>>
>>> Regards,
>>> Ankur Seth
>>>
>>>
>>> On Sat, Sep 7, 2013 at 2:11 PM, Rui Barradas <ruipbarradas at sapo.pt>
>>> wrote:
>>>
>>> Hello,
>>>>
>>>> Try the following.
>>>>
>>>> dat <- read.table(text = "
>>>>
>>>> Date Value
>>>> 08/01/2013 100
>>>> 08/02/2013 100.5
>>>> 08/03/2013 102
>>>> ", header = TRUE)
>>>>
>>>>
>>>> dat$New <- c(NA, diff(dat$Value))
>>>> dat
>>>>
>>>>
>>>> Hope this helps,
>>>>
>>>> Rui Barradas
>>>>
>>>> Em 07-09-2013 06:06, Ankur Seth escreveu:
>>>>
>>>> Hello All,
>>>>>
>>>>> I am trying to build a model in R. I am facing the following problem...
>>>>>
>>>>> My Data Frame contains the following data...
>>>>>
>>>>> Date Value
>>>>> 08/01/2013 100
>>>>> 08/02/2013 100.5
>>>>> 08/03/2013 102
>>>>> ....
>>>>>
>>>>> Now I want to add a column to this data frame where New Column Value =
>>>>> Difference of two subsequent observations. For Eg. on 08/02/2013 the new
>>>>> value = 100.5 - 100=0.5
>>>>>
>>>>> I want to do this dynamically such that if I change the value in Value
>>>>> column the new column should recalculate automatically.
>>>>>
>>>>> Is there a way to do this in R?
>>>>>
>>>>> Regards,
>>>>> Ankur Seth
>>>>>
>>>>> [[alternative HTML version deleted]]
>>>>>
>>>>> ______________________________****________________
>>>>> R-help at r-project.org mailing list
>>>>> https://stat.ethz.ch/mailman/****listinfo/r-help<https://stat.ethz.ch/mailman/**listinfo/r-help>
>>>>> <https://stat.**ethz.ch/mailman/listinfo/r-**help<https://stat.ethz.ch/mailman/listinfo/r-help>
>>>>>>
>>>>> PLEASE do read the posting guide http://www.R-project.org/**
>>>>> posting-guide.html <http://www.R-project.org/**posting-guide.html<http://www.R-project.org/posting-guide.html>
>>>>>>
>>>>>
>>>>> and provide commented, minimal, self-contained, reproducible code.
>>>>>
>>>>>
>>>>>
>>>
>>>
>
>
More information about the R-help
mailing list