[R] How to catch data from the different dataframes and lm problem?
leeznar
leeznar at yahoo.com.tw
Fri Dec 28 04:51:51 CET 2007
Dear all:
I am a new R-user and I have 2 questions
about it.
1) I have a dataframe. Based on “formulation” and “subject”, a
dataframe is split into 4 dataframes. The
example is as follows. Moreover, I want
to calculate “test” value for these 4 dataframes. My question is that the “test” values not
correct and I do not know where the problem is.
2) There are 12 “test” (y) values from 1). Then, I want to model the relationship
between “concentration” (X) and “test” (Y) by fitting the linear regression,
such lm(Y~X) and this is my target. I
think that if I can catch “test” (Y) values and “concentration” (X) values into
a dataframe, then I can carry out regression. So, how to catch all “test” values from
different dataframes? Or does anyone
have better way to get this target?
Example:
w<-(c(1,1,1,2,2,2,1,1,1,2,2,2))
y<-(c(1,1,1,1,1,1,2,2,2,2,2,2))
z<-(c(1,2,3,1,2,3,1,2,3,1,2,3))
c<-(c(0.1,10,20,0.3,2.5,6,20,25,60,35,40,45))
df<-data.frame(formulation=y, subject=w,
time=z, concentration=c)
A.split<-split(df, list(df$formulation,
df$subject) )
for (j in 1:length(A.split)){
test <- 0
for(i in
2:length(A.split[[j]][["time"]])){
test[i] <- (A.split[[1]][["time"]][i] -
A.split[[1]][["time"]][i-1]) *
(A.split[[1]][["concentration"]][i] -
A.split[[1]][["concentration"]][i-1])* 0.5
test[i]<-test[i]+test[i-1]
}
output<-data.frame(A.split[[j]][["subject"]],A.split[[j]][["formulation"]],A.split[[j]][["time"]],A.split[[j]][["concentration"]],test)
colnames(output)<-list("subject","formulation","time","concentration","test")
show(output)
}
Best regards,
Hsin-Ya Lee
_____________________________________________________________________________________
馬上體驗全新Yahoo!奇摩電子信箱2.0。 http://tw.mg0.mail.yahoo.com/dc/landing
More information about the R-help
mailing list