Hi all, I have a very basic doubt -- but still, I am a newby! My question is about referring to the previous row: in a sample as the following... ID X1 X2 1 A 12 2 A 6 3 A 10 1 B 17 2 B 19 1 C 22 1 D 13 2 D 19 3 D 21 ... I would like to create a dummy variable equal to 1 whenever the value of ID of the current row is lower or equal than the value of ID of the previous row -- check the new vector X3 I'd like to obtain: ID X1 X2 X3 1 A 12 0 2 A 6 0 3 A 10 0 1 B 17 1 2 B 19 0 1 C 22 1 1 D 13 1 2 D 19 0 3 D 21 0 I have searched a lot without finding a decent and working solution. I suppose it is just some basic matter of indexing language, something like X3<- as.numeric ( ID[n] <= ID[n-1]) but it is not so simple! thanks! Paolo
Hello, Try the following. dat$X3 <- c(0L, dat$ID[-1] <= dat$ID[-nrow(dat)]) Hope this helps, Rui Barradas Em 07-01-2013 13:33, Paolo Donatelli escreveu:> Hi all, > > I have a very basic doubt -- but still, I am a newby! > > My question is about referring to the previous row: in a sample as the > following... > > ID X1 X2 > 1 A 12 > 2 A 6 > 3 A 10 > 1 B 17 > 2 B 19 > 1 C 22 > 1 D 13 > 2 D 19 > 3 D 21 > > ... I would like to create a dummy variable equal to 1 whenever the > value of ID of the current row is lower or equal than the value of ID > of the previous row -- check the new vector X3 I'd like to obtain: > > ID X1 X2 X3 > 1 A 12 0 > 2 A 6 0 > 3 A 10 0 > 1 B 17 1 > 2 B 19 0 > 1 C 22 1 > 1 D 13 1 > 2 D 19 0 > 3 D 21 0 > > I have searched a lot without finding a decent and working solution. I > suppose it is just some basic matter of indexing language, something > like > > X3<- as.numeric ( ID[n] <= ID[n-1]) > > but it is not so simple! > > > thanks! > Paolo > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
try this> x <- read.table(text = 'ID X1 X2+ 1 A 12 + 2 A 6 + 3 A 10 + 1 B 17 + 2 B 19 + 1 C 22 + 1 D 13 + 2 D 19 + 3 D 21', header = TRUE, as.is = TRUE)> x$X3 <- c(0, diff(x$ID) <= 0) > xID X1 X2 X3 1 1 A 12 0 2 2 A 6 0 3 3 A 10 0 4 1 B 17 1 5 2 B 19 0 6 1 C 22 1 7 1 D 13 1 8 2 D 19 0 9 3 D 21 0 On Mon, Jan 7, 2013 at 8:33 AM, Paolo Donatelli <donatellipaolo at gmail.com> wrote:> Hi all, > > I have a very basic doubt -- but still, I am a newby! > > My question is about referring to the previous row: in a sample as the > following... > > ID X1 X2 > 1 A 12 > 2 A 6 > 3 A 10 > 1 B 17 > 2 B 19 > 1 C 22 > 1 D 13 > 2 D 19 > 3 D 21 > > ... I would like to create a dummy variable equal to 1 whenever the > value of ID of the current row is lower or equal than the value of ID > of the previous row -- check the new vector X3 I'd like to obtain: > > ID X1 X2 X3 > 1 A 12 0 > 2 A 6 0 > 3 A 10 0 > 1 B 17 1 > 2 B 19 0 > 1 C 22 1 > 1 D 13 1 > 2 D 19 0 > 3 D 21 0 > > I have searched a lot without finding a decent and working solution. I > suppose it is just some basic matter of indexing language, something > like > > X3<- as.numeric ( ID[n] <= ID[n-1]) > > but it is not so simple! > > > thanks! > Paolo > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.-- Jim Holtman Data Munger Guru What is the problem that you are trying to solve? Tell me what you want to do, not how you want to do it.
On Jan 7, 2013, at 5:33 AM, Paolo Donatelli wrote:> Hi all, > > I have a very basic doubt -- but still, I am a newby! > > My question is about referring to the previous row: in a sample as the > following... > > ID X1 X2 > 1 A 12 > 2 A 6 > 3 A 10 > 1 B 17 > 2 B 19 > 1 C 22 > 1 D 13 > 2 D 19 > 3 D 21 > > ... I would like to create a dummy variable equal to 1 whenever the > value of ID of the current row is lower or equal than the value of ID > of the previous row -- check the new vector X3 I'd like to obtain: > > ID X1 X2 X3 > 1 A 12 0 > 2 A 6 0 > 3 A 10 0 > 1 B 17 1 > 2 B 19 0 > 1 C 22 1 > 1 D 13 1 > 2 D 19 0 > 3 D 21 0Something like (untested): dfrm$X3 <- c(0, as.numeric( diff(dfrm$ID) <= 0 ) ) That might be faster than this sort of untested strategy: ... <- with(dfrm, c( 0 , as.numeric( ID[2:nrow(dfrm)] <= ID[1: (nrow(dfrm)-1] ) ) ) In my newbie days I thought a function named `lag` would do it, but discovered it was only working on ts-class objects.> > I have searched a lot without finding a decent and working solution. > I Adding > suppose it is just some basic matter of indexing language, something > like > > X3<- as.numeric ( ID[n] <= ID[n-1])An explicit sequence rather than using mathematical notation is needed. And if you are using dataframes, you should not be using `attach`. That X3 would not be constructed in the dataframe. -- David Winsemius, MD Alameda, CA, USA
On 07-01-2013, at 14:33, Paolo Donatelli <donatellipaolo at gmail.com> wrote:> Hi all, > > I have a very basic doubt -- but still, I am a newby! > > My question is about referring to the previous row: in a sample as the > following... > > ID X1 X2 > 1 A 12 > 2 A 6 > 3 A 10 > 1 B 17 > 2 B 19 > 1 C 22 > 1 D 13 > 2 D 19 > 3 D 21 > > ... I would like to create a dummy variable equal to 1 whenever the > value of ID of the current row is lower or equal than the value of ID > of the previous row -- check the new vector X3 I'd like to obtain: > > ID X1 X2 X3 > 1 A 12 0 > 2 A 6 0 > 3 A 10 0 > 1 B 17 1 > 2 B 19 0 > 1 C 22 1 > 1 D 13 1 > 2 D 19 0 > 3 D 21 0 > > I have searched a lot without finding a decent and working solution. I > suppose it is just some basic matter of indexing language, something > like > > X3<- as.numeric ( ID[n] <= ID[n-1])Assuming your data are in a data.frame called dat this will work dat$X3 <- c(0, diff(dat$ID)<=0) Berend
On 07/01/2013 8:33 AM, Paolo Donatelli wrote:> Hi all, > > I have a very basic doubt -- but still, I am a newby! > > My question is about referring to the previous row: in a sample as the > following... > > ID X1 X2 > 1 A 12 > 2 A 6 > 3 A 10 > 1 B 17 > 2 B 19 > 1 C 22 > 1 D 13 > 2 D 19 > 3 D 21 > > ... I would like to create a dummy variable equal to 1 whenever the > value of ID of the current row is lower or equal than the value of ID > of the previous row -- check the new vector X3 I'd like to obtain: > > ID X1 X2 X3 > 1 A 12 0 > 2 A 6 0 > 3 A 10 0 > 1 B 17 1 > 2 B 19 0 > 1 C 22 1 > 1 D 13 1 > 2 D 19 0 > 3 D 21 0 > > I have searched a lot without finding a decent and working solution. I > suppose it is just some basic matter of indexing language, something > like > > X3<- as.numeric ( ID[n] <= ID[n-1]) > > but it is not so simple!Negative indexing lets you leave out an entry, so x[-1] leaves out the first entry, and x[-length(x)] leaves out the last one. To talk about previous entries, you need to do something about the fact that the first row has no previous entry. You gave X3[1] the value 0, suggesting that you want to implicitly have the "zeroth" row to have the smallest possible value. So prevID <- c( -Inf, ID[-length(ID)] ) X3 <- as.numeric( ID <= prevID ) should do what you want. Duncan Murdoch
> I suppose it is just some basic matter of indexing language, something like > > X3<- as.numeric ( ID[n] <= ID[n-1]) > > but it is not so simple!If you first define 'n' as n <- seq_along(ID)[-1] # 2:length(ID) then that code works.> with(Data, { n <- seq_along(ID)[-1] ; as.numeric ( ID[n] <= ID[n-1]) })[1] 0 0 1 0 1 1 0 0 You may want to put a NA at the start of the result so it has the same length as the other columns in the data.frame. I usually find it more convenient to leave things like ID[n]<=ID[n-1] as logical variables instead of converting them to numbers. Most functions do the conversion implicitly if required and leaving them as logicals helps me remember the meaning. Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf > Of Paolo Donatelli > Sent: Monday, January 07, 2013 5:34 AM > To: r-help at r-project.org > Subject: [R] Refer to previous row > > Hi all, > > I have a very basic doubt -- but still, I am a newby! > > My question is about referring to the previous row: in a sample as the > following... > > ID X1 X2 > 1 A 12 > 2 A 6 > 3 A 10 > 1 B 17 > 2 B 19 > 1 C 22 > 1 D 13 > 2 D 19 > 3 D 21 > > ... I would like to create a dummy variable equal to 1 whenever the > value of ID of the current row is lower or equal than the value of ID > of the previous row -- check the new vector X3 I'd like to obtain: > > ID X1 X2 X3 > 1 A 12 0 > 2 A 6 0 > 3 A 10 0 > 1 B 17 1 > 2 B 19 0 > 1 C 22 1 > 1 D 13 1 > 2 D 19 0 > 3 D 21 0 > > I have searched a lot without finding a decent and working solution. I > suppose it is just some basic matter of indexing language, something > like > > X3<- as.numeric ( ID[n] <= ID[n-1]) > > but it is not so simple! > > > thanks! > Paolo > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
Apparently Analagous Threads
- How to get predicted values of y for different x values?
- Basic loop programming
- Conditional replacement of NA depending on value in the previous column
- subset from a dataset after comparing its one column to a related vector
- how to create a substraction matrix (subtract a row of every column from the same row in other columns)