我想添加一個新的列,這取決於另一個列的行組合。如何加入具有共同列的兩個數據框?
例如可以說我有一個數據幀象下面這樣:
library(dplyr)
library(minpack.lm)
library(broom)
No = c(replicate(1,rep(letters[1:6],each=10)))
ACME <- as.character(rep(rep(c(78,110),each=10),times=3))
ARGON <- as.character(rep(rep(c(256,320,384),each=20),times=1))
V <- rep(c(seq(2,40,length.out=5),seq(-2,-40,length.out=5)),times=1)
DQ0 = c(replicate(2, sort(runif(10,0.001,1))))
direc <- rep(rep(c("North","South"),each=5),times=6)
df <- data.frame(No,ACME,ARGON,V,DQ0,direc)
>df
No ACME ARGON V DQ0 direc
1 a 78 256 2.0 0.07532351 North
2 a 78 256 11.5 0.13785481 North
3 a 78 256 21.0 0.27397961 North
4 a 78 256 30.5 0.44296243 North
5 a 78 256 40.0 0.45721902 North
6 a 78 256 -2.0 0.68077463 North
7 a 78 256 -11.5 0.68764283 North
8 a 78 256 -21.0 0.76284209 North
9 a 78 256 -30.5 0.81040056 North
10 a 78 256 -40.0 0.95336230 North
11 b 110 256 2.0 0.04190305 South
12 b 110 256 11.5 0.17484353 South
13 b 110 256 21.0 0.22409319 South
----------------
我適合這個df
使用nlsLM
溫控功能從minpack.lm
包
- >配合部
nls_fit=nlsLM(DQ0~ifelse(df$direc=="North"&V<J1, exp((-t_pw)/f0*exp(-del1*(1-V/J1)^2)),1)*ifelse(df$direc=="South"&V>J2, exp((-t_pw)/f0*exp(-del2*(1-V/J2)^2)),1)
,data=df,start=c(del1=1,J1=15,del2=1,J2=-15),trace=T)
後擬合我想創建一個新的數據框df_new
與新列名爲address
df_new<- df%>%
group_by(No)%>%
do(data.frame(model=tidy(nls_fit)))%>% # **this part is related fit fitting result. After this process I got "model.term" and "model.estimate"** columns and in the next step I renamed them.
select_("delta"="model.term","value"= "model.estimate")%>%
filter(delta%in%c("del1","del2"))%>% #**I filter some fitting parameters**
mutate(adress=interaction(ACME,ARGON))%>% #this part is not working
ungroup
我收到錯誤,說
錯誤:不兼容的大小(%d),預計%d(組大小)或1
最後我有一個這樣的輸出沒有mutatate
部分
df_new
No delta value
1 a del1 1.479056
2 a del2 1.016404
3 b del1 1.479056
4 b del2 1.016404
5 c del1 1.479056
6 c del2 1.016404
7 d del1 1.479056
8 d del2 1.016404
9 e del1 1.479056
10 e del2 1.016404
11 f del1 1.479056
12 f del2 1.016404
我希望得到像這樣的東西;
No delta value adress
1 a del1 1.479056 78.256
2 a del2 1.016404 78.256
3 b del1 1.479056 110.256
4 b del2 1.016404 110.256
5 c del1 1.479056 78.320
6 c del2 1.016404 78.320
7 d del1 1.479056 110.320
8 d del2 1.016404 110.320
9 e del1 1.479056 78.384
10 e del2 1.141958 78.384
11 f del1 1.019201 110.384
12 f del2 1.141958 110.384
哪裏'nls_fit'從何而來?請包括您使用的軟件包。 – Jaap
@Jaap你想讓我添加配件嗎? 'nls_fit'來自'minpack.lm'包。我安排了一些'df'的列,並將它們排除在外,因爲它們與這裏的問題無關。我把輸出'df_new'放在這裏。 – Alexander
@Jaap好的,我附上了相關的軟件包。 – Alexander