[R-sig-Geo] problem to caclulate in a data frame
Torleif Markussen Lunde
Torleif.Lunde at cih.uib.no
Tue Mar 10 18:56:20 CET 2009
Don't worry. Everyone has to start somewhere. If I understand your question correctly, this is what you want.
##START##
#Make a demonstration data frame
data.df <- data.frame(ID=factor(c(1, 2, 3, 4, 5)), x=c(25.8, 45, 24.1, 54.8, 41.9))
#If you want to keep the original data frame, make a new one equal to data.df
CorrectIndex.df <- data.df
#COlumn x in CorrectIndex.df should be equal to data.df$x - 2
CorrectIndex.df$x <- data.df$x - 2
##END##
Here 2 is recycled for each row, and is actually treated as a vector. What it actually does is to create a vector c(2,2,2,2,2) and subtract it. You could also try data.df$x - c(2, 1) and look at the result. When you feel more into R, you could also look at ?apply
sapply(data.df$x, function (x) x-2)
When that is said, I guess this question would be more suitable for the R-help mailinglist.
What you actually did was to ask R to sort (and subset) the rows in the dataframe like this data.df[c(5, 4, 3, 1, 2), c("ID", "x")]. You also say it should be done for both ID and x. Try the example in the previous sentence, and you will understand..
Best wishes, and good luck
Torleif
________________________________
Fra: r-sig-geo-bounces at stat.math.ethz.ch på vegne av gianni lavaredo
Sendt: ti 10.03.2009 17:28
Til: r-sig-geo at stat.math.ethz.ch
Emne: [R-sig-Geo] problem to caclulate in a data frame
Thanks for attention. I am reading the manual of R to work inside the
dataframe. I write this code to subtract the value 2 at the column x
values
I import the data, see the structures of my data frame
> str(Soil_pollution)
'data.frame': 912 obs. of 2 variables:
$ ID: Factor w/ 912 levels "1","2","3","4",..: 1 2 3 4 5 6 7 8 9 10 ...
$ x : num 22.8 23 53.5 39.7 27.5 ...
# subtract index = 2 at all values
indexcorrect_Soil_pollution <- Soil_pollution[Soil_pollution$x - 2
,c("ID","x")]
> str(indexcorrect_Soil_pollution)
'data.frame': 912 obs. of 2 variables:
$ ID: Factor w/ 912 levels "1","2","3","4",..: 20 21 51 37 25 45 37 24 36
39 ...
$ x : num 25.8 45 24.1 54.8 41.9 ...
there is a modification of the data frame but sorry I don't understand where
is the problem in the code. I spent all my afternoon to read. Sorry if I
sent this email
Gianni
[[alternative HTML version deleted]]
_______________________________________________
R-sig-Geo mailing list
R-sig-Geo at stat.math.ethz.ch
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
More information about the R-sig-Geo
mailing list