Re: [R] Calculating column differences

2021-03-24 Thread Jeff Reichman
:41 PM To: r-help@r-project.org Cc: reichm...@sbcglobal.net; Gerrit Eichner Subject: Re: [R] Calculating column differences More correctly, with an initial "NA" value in the "diff" column: > df <- data.frame(ID=1:5,Score=4*2:6) > df1 <- rbind(c(0,0), df) > cbind(

Re: [R] Calculating column differences

2021-03-24 Thread William Michels via R-help
More correctly, with an initial "NA" value in the "diff" column: > df <- data.frame(ID=1:5,Score=4*2:6) > df1 <- rbind(c(0,0), df) > cbind(df1, "diff"=c(NA, diff(df1$Score)) ) ID Score diff 1 0 0 NA 2 1 88 3 2124 4 3164 5 4204 6 5244 > HTH,

Re: [R] Calculating column differences

2021-03-24 Thread Jeff Reichman
ubject: Re: [R] Calculating column differences Dear Jeff, read diff's help page, and you'll find out what is wrong with your expectation. What do think diff(df$Score) should give for the first element in df$Score?? Hth --

Re: [R] Calculating column differences

2021-03-24 Thread William Michels via R-help
Dear Jeff, Rather than diff-ing a linear vector you're trying to diff values from two different rows. Also you indicate that you want to place the diff-ed value in the 'lower' row of a new column. Try this (note insertion of an initial "zero" row): > df <- data.frame(ID=1:5,Score=4*2:6) > df1 <-

Re: [R] Calculating column differences

2021-03-24 Thread Gerrit Eichner
Dear Jeff, read diff's help page, and you'll find out what is wrong with your expectation. What do think diff(df$Score) should give for the first element in df$Score?? Hth -- Gerrit - Dr. Gerrit Eichner

[R] Calculating column differences

2021-03-24 Thread Jeff Reichman
r-help forum I'm trying to calculate the diff between two rows and them mutate the difference into a new column. I'm using the diff function but not giving me what I want. df <- data.frame(ID=1:5,Score=4*2:6) What a want where ID Score diff 1 1 8 8 2 212 4