Hi, i have that table Thesis Day A B C 1 0 83.43 90.15 22.97 1 0 85.50 94.97 16.62 1 0 83.36 95.38 20.70 1 0 84.47 92.16 23.58 1 0 83.98 95.33 19.39 1 0 82.86 93.78 24.55 1 0 83.39 92.67 19.56 1 0 85.17 95.24 17.95 1 0 81.62 93.32 28.49 1 0 82.99 92.85 19.73 1 0 81.11 95.67 27.20 1 0 83.39 94.69 16.51 1 0 79.56 89.87 30.39 1 0 80.54 93.32 21.76 1 0 82.11 92.58 22.17 1 14 85.65 94.00 19.19 1 14 85.06 92.44 20.44 1 14 83.97 91.39 24.38 1 14 84.61 91.97 19.44 1 14 85.13 90.59 25.30 1 14 84.81 91.01 19.80 1 14 84.52 94.06 18.77 1 14 84.30 94.49 24.90 1 14 84.74 91.32 20.35 1 14 84.08 94.12 22.96 1 14 84.50 94.25 19.95 1 14 84.02 94.74 20.35 1 14 85.30 92.82 21.12 1 14 85.08 91.14 24.16 1 14 85.21 95.69 18.17 etc etc etc etc etc 2 0 83.43 90.15 22.97 2 0 85.50 94.97 16.62 2 0 83.36 95.38 20.70 2 0 84.47 92.16 23.58 2 0 83.98 95.33 19.39 2 0 82.86 93.78 24.55 2 0 83.39 92.67 19.56 2 0 85.17 95.24 17.95 2 0 81.62 93.32 28.49 2 0 82.99 92.85 19.73 2 0 81.11 95.67 27.20 2 0 83.39 94.69 16.51 2 0 79.56 89.87 30.39 2 0 80.54 93.32 21.76 2 0 82.11 92.58 22.17 2 14 84.48 91.23 20.44 2 14 85.22 93.08 22.54 2 14 83.89 92.74 25.11 etc etc etc etc etc I need to subtract from every number the other numbers of the same thesis and same day. Example: A(row1) - A (row2) (same for B and C) A(row1) - A (row3) etc until the last Thesis 1 and Day 0 A(row2) - A (row3) etc etc until the last Thesis 1 and Day 0 Same for the others theses and days. How can i do that? Sorry for my english. -- View this message in context: http://r.789695.n4.nabble.com/test-tp3217329p3217329.html Sent from the R help mailing list archive at Nabble.com.
Hi:
If I understood you correctly, the following may work:
# Utility function to compute all pairwise differences of a vector:
# The upper triangle subtracts x_i - x_j for j > i; if you want it the
# other way around, use lower.tri() instead of upper.tri().
# Coercion to vector means that the differences from x_1 appear
# first, followed by those from x_2, then x_3, etc.
subtfun <- function(x) {
u <- outer(x, x, '-')
as.vector(u[upper.tri(u)])
}
# To apply it to each of A, B, C in each Thesis:Day subgroup,
# here's one way with function ddply() in the plyr package:
library(plyr)
v <- ddply(df, .(Thesis, Day), numcolwise(subtfun))
head(v)
HTH,
Dennis
On Fri, Jan 14, 2011 at 1:17 AM, romzero <romzero@yahoo.it> wrote:
>
> Hi, i have that table
>
>
>
> Thesis Day A B C
> 1 0 83.43 90.15 22.97
> 1 0 85.50 94.97 16.62
> 1 0 83.36 95.38 20.70
> 1 0 84.47 92.16 23.58
> 1 0 83.98 95.33 19.39
> 1 0 82.86 93.78 24.55
> 1 0 83.39 92.67 19.56
> 1 0 85.17 95.24 17.95
> 1 0 81.62 93.32 28.49
> 1 0 82.99 92.85 19.73
> 1 0 81.11 95.67 27.20
> 1 0 83.39 94.69 16.51
> 1 0 79.56 89.87 30.39
> 1 0 80.54 93.32 21.76
> 1 0 82.11 92.58 22.17
> 1 14 85.65 94.00 19.19
> 1 14 85.06 92.44 20.44
> 1 14 83.97 91.39 24.38
> 1 14 84.61 91.97 19.44
> 1 14 85.13 90.59 25.30
> 1 14 84.81 91.01 19.80
> 1 14 84.52 94.06 18.77
> 1 14 84.30 94.49 24.90
> 1 14 84.74 91.32 20.35
> 1 14 84.08 94.12 22.96
> 1 14 84.50 94.25 19.95
> 1 14 84.02 94.74 20.35
> 1 14 85.30 92.82 21.12
> 1 14 85.08 91.14 24.16
> 1 14 85.21 95.69 18.17
> etc etc etc etc etc
> 2 0 83.43 90.15 22.97
> 2 0 85.50 94.97 16.62
> 2 0 83.36 95.38 20.70
> 2 0 84.47 92.16 23.58
> 2 0 83.98 95.33 19.39
> 2 0 82.86 93.78 24.55
> 2 0 83.39 92.67 19.56
> 2 0 85.17 95.24 17.95
> 2 0 81.62 93.32 28.49
> 2 0 82.99 92.85 19.73
> 2 0 81.11 95.67 27.20
> 2 0 83.39 94.69 16.51
> 2 0 79.56 89.87 30.39
> 2 0 80.54 93.32 21.76
> 2 0 82.11 92.58 22.17
> 2 14 84.48 91.23 20.44
> 2 14 85.22 93.08 22.54
> 2 14 83.89 92.74 25.11
> etc etc etc etc etc
>
> I need to subtract from every number the other numbers of the same thesis
> and same day.
>
> Example:
> A(row1) - A (row2) (same for B and C)
> A(row1) - A (row3)
> etc until the last Thesis 1 and Day 0
> A(row2) - A (row3)
> etc etc until the last Thesis 1 and Day 0
>
> Same for the others theses and days.
>
> How can i do that?
>
> Sorry for my english.
> --
> View this message in context:
> http://r.789695.n4.nabble.com/test-tp3217329p3217329.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help@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.
>
[[alternative HTML version deleted]]
Dear Romezo,
a solution maybe not that elegant and effective, but seems to work:
test_calculate<-function() {
tarrow<-1
TARGET<-data.frame("Thesis"=0,
"Day"=0,"A"=0,"B"=0,"C"=0)
for (i in c(unique(my.data$Thesis))) {
for (j in c(unique(my.data$Day[ my.data$Thesis==i ]))) {
TEMPDF<-subset(my.data, Thesis==i & Day==j)
for (k in c(1:(nrow(TEMPDF)-1))) {
for (l in c((k+1):nrow(TEMPDF))) {
TARGET[tarrow,]<-cbind(i,j,(TEMPDF[k,3]-TEMPDF[l,3]),
(TEMPDF[k,4]-TEMPDF[l,4]), (TEMPDF[k,5]-TEMPDF[l,5]))
tarrow<-tarrow+1
}
}
}
}
print(TARGET)
}
(Please note: my.data is your original data frame)
Kind regards,
Kimmo
Here is a more elegant solution using the plyr package.
my.data <- expand.grid(Thesis = 1:3, Day = c(0, 14), Run = 1:10)
my.data$A <- rpois(nrow(my.data), 10)
my.data$B <- rpois(nrow(my.data), 10)
my.data$C <- rpois(nrow(my.data), 10)
library(plyr)
ddply(my.data, .(Thesis, Day), function(x){
Baseline <- unlist(x[1, c("A", "B", "C")])
data.frame(t(apply(x[-1, c("A", "B", "C")], 1,
function(z){z - Baseline})))
})
Best regards,
Thierry
----------------------------------------------------------------------------
ir. Thierry Onkelinx
Instituut voor natuur- en bosonderzoek
team Biometrie & Kwaliteitszorg
Gaverstraat 4
9500 Geraardsbergen
Belgium
Research Institute for Nature and Forest
team Biometrics & Quality Assurance
Gaverstraat 4
9500 Geraardsbergen
Belgium
tel. + 32 54/436 185
Thierry.Onkelinx at inbo.be
www.inbo.be
To call in the statistician after the experiment is done may be no more than
asking him to perform a post-mortem examination: he may be able to say what the
experiment died of.
~ Sir Ronald Aylmer Fisher
The plural of anecdote is not data.
~ Roger Brinner
The combination of some data and an aching desire for an answer does not ensure
that a reasonable answer can be extracted from a given body of data.
~ John Tukey
> -----Oorspronkelijk bericht-----
> Van: r-help-bounces at r-project.org
> [mailto:r-help-bounces at r-project.org] Namens K. Elo
> Verzonden: vrijdag 14 januari 2011 14:52
> Aan: r-help at r-project.org
> Onderwerp: Re: [R] test
>
> Dear Romezo,
>
> a solution maybe not that elegant and effective, but seems to work:
>
> test_calculate<-function() {
> tarrow<-1
> TARGET<-data.frame("Thesis"=0,
"Day"=0,"A"=0,"B"=0,"C"=0)
> for (i in c(unique(my.data$Thesis))) {
> for (j in c(unique(my.data$Day[ my.data$Thesis==i ]))) {
> TEMPDF<-subset(my.data, Thesis==i & Day==j)
> for (k in c(1:(nrow(TEMPDF)-1))) {
> for (l in c((k+1):nrow(TEMPDF))) {
> TARGET[tarrow,]<-cbind(i,j,(TEMPDF[k,3]-TEMPDF[l,3]),
> (TEMPDF[k,4]-TEMPDF[l,4]), (TEMPDF[k,5]-TEMPDF[l,5]))
> tarrow<-tarrow+1
> }
> }
> }
> }
> print(TARGET)
> }
>
> (Please note: my.data is your original data frame)
>
> Kind regards,
> Kimmo
>
> ______________________________________________
> 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.
>
Thank you all for the precious help.
Finally i could start the writing of a first part of my script, but now i
have a new question for you.
Need i to repeat the write.table portion for all the 15 lines or can i use a
"short cut"?
Example:
file.open <- "C:\\test.txt"
file.save <- "C:\\results.txt"
my.data <- read.table(file, header=T)
library(plyr)
write.table(ddply(my.data, .(Thesis, Day), function(x){
Baseline <- unlist(x[1, c("A", "B",
"C")])
data.frame(t(apply(x[-1, c("A", "B",
"C")], 1, function(z){z -
Baseline})))
}), file = file.save, row.names = F)
write.table(ddply(my.data, .(Thesis, Day), function(x){
Baseline <- unlist(x[2, c("A", "B",
"C")])
data.frame(t(apply(x[-1:-2, c("A", "B",
"C")], 1, function(z){z -
Baseline})))
}), file = file.save, append = T, row.names = F, col.names = F)
etc etc
Thanks again for the help.
Best regards,
Roberto.
--
View this message in context:
http://r.789695.n4.nabble.com/Comparison-of-numbers-in-a-table-tp3217329p3218524.html
Sent from the R help mailing list archive at Nabble.com.
Seemingly Similar Threads
- Change global env variables from within a function
- Extract letters from a column
- lattice multiple y-scale possible?
- Creating a "shifted" month (one that starts not on the first of each month but on another date)
- function similar to "get" that works for both an object, and elements of an object?