: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(
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, Bi
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??
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 <-
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 Ma
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
3
6 matches
Mail list logo