나는이 선형 회귀에 도움이 될 것입니다 방법을 알고하지 않습니다하지만 당신은 그런 일 할 수있는 : lapply
를 사용
df <- read.table(header=T, text="Assay Sample Dilution meanresp number
1 S 0.25 68.55 1
1 S 0.50 54.35 2
1 S 1.00 44.75 3")
을 :
이 경우
> lapply(2:nrow(df), function(x) df[(x-1):x,])
[[1]]
Assay Sample Dilution meanresp number
1 1 S 0.25 68.55 1
2 1 S 0.50 54.35 2
[[2]]
Assay Sample Dilution meanresp number
2 1 S 0.5 54.35 2
3 1 S 1.0 44.75 3
당신이 특정 열을 원하는 계속해서 연속 행을 선택하면 다음과 같이 선택할 수 있습니다.
> lapply(2:nrow(df), function(x) df[(x-1):x, c('Dilution','meanresp')])
[[1]]
Dilution meanresp
1 0.25 68.55
2 0.50 54.35
[[2]]
Dilution meanresp
2 0.5 54.35
3 1.0 44.75
편집
는 선형 회귀 분석을 수행하려면 다른 lapply
는 할 충분하다 :
a <- lapply(2:nrow(df), function(x) df[(x-1):x, c('Dilution','meanresp')])
b <- lapply(a,function(x) lm(Dilution~meanresp,data=x))
>b
[[1]]
Call:
lm(formula = Dilution ~ meanresp, data = x)
Coefficients:
(Intercept) meanresp
1.45687 -0.01761
[[2]]
Call:
lm(formula = Dilution ~ meanresp, data = x)
Coefficients:
(Intercept) meanresp
3.33073 -0.05208
그냥 기울기하려는 경우 또는 :
b <- lapply(a, function(x) {
d <- lm(Dilution~meanresp,data=x)
coefficients(summary(d))[2,1]
})
> b
[[1]]
[1] -0.01760563
[[2]]
[1] -0.05208333
을 잘 모르겠어요 무엇 당신은 그 결과로 기대합니다. 'diff (meanresp)/diff (Dilution)'('Assay'와'Sample'로 그룹화)와 같은 것? – Roland
수 [this] (http://stackoverflow.com/questions/26755653/r-how-to-write-a-for-loop-that-reads-every-two-lines-in-a-matrix/26756580# 26756580) 도움이 필요하십니까? (두 행을 연속적으로 선택하기 위해) – Cath
나는 이것을 수행하는 것을 목표로하고있다 : ddply (.data = data, .variables = '연속적인 2 행의 부분 집합', .fun = '선형 모델 함수') 하위 집합은 문제가있는 부분입니다. 이게 말이 돼? – Kabau