2016-12-26 152 views
0

我有兩個具有共享因子級別(「Auction_ID」)的數據幀(df,df5)。 so df有num.bidders和res.bid和Auction_ID。 df5,有bid.points,Auction_ID。lapply()和spline()在R中的兩個數據幀中,不合並

我用smooth.splines()函數來獲得的樣條估計,和我保存它作爲DF新列(我不知道我是否應該將其保存在DF5)

spline <- smooth.spline(df$c_bidders,df$res.bid) 

的問題是如何對每個級別的df $ spline1和df5 $ bid.points使用predict()函數。我嘗試使用lapply併發送df,df5作爲函數的輸入數據,但似乎我無法做到。 喜歡:

lapply(df,df5, function(t,t1) 
    { 
    tt<-predict(t$spline,t1$bid.points,deriv=0)$y 
    return(tt)} 
    ) 

我不知道如果我介紹一個列表變量,這能否幫助?

如果我使用合併(DF,DF5,通過= 「Auction_ID」),那麼我結束了非常大的數據幀:

str(df1): 
    Classes ‘tbl_df’, ‘tbl’ and 'data.frame': 3967 obs. of 17 variables: 

    str(df5) 
    'data.frame': 18338 obs. of 2 variables: 

    x <- merge(df5, df1, by = "Auction_ID") 
    str(x) 
    'data.frame': 501367 obs. of 19 variables: 

(合併()的 「所有」 選項已經嘗試等。 all.y = TRUE ...給出了相同數量的OBS的,這是不利於我的目的。

+0

原始WinnersCurse.txt R代碼是否完全符合您的需求,因爲您的翻譯看起來並不一致?例如,* AuctionID *未在原始引用。他們使用的 – Parfait

+0

:代碼中的auctionid ... –

回答

0

是你不想要對付大DF與50,000列的問題?

也許合併(aka join)並不是你所需要的,也許你只需要使用「match」函數來實質上執行一個vlookup a nd將df $ spline1的每個值與df5 $ bid.points(基於拍賣ID)的每個對應值相匹配。

看看這種方式更適合你的目的:

# assuming df5 is the target df: 
df5$spline1 <- df$spline1[match(df$Auction_ID,df5$Auction_ID)] 

## OR 

# assuming df is the target df: 
df$bid.points <- df5$bid.points[match(df$Auction_ID,df5$Auction_ID)] 
0

考慮使用Map通過兩個dataframes返回從predict()返回值的列表:

列表返回

Map(function(t, t1) predict(t$spline, t1$bid.points,deriv=0)$y, df, df5) 

以上將等同於將第二個數據框作爲第三個參數傳入lapply()

lapply(df, function(t,t1) { 
    predict(t$spline, t1$bid.points, deriv=0)$y 
}, df5) 

矩陣返回

或者,使用sapply()它返回一個矩陣:

sapply(df, function(t,t1) { 
    predict(t$spline, t1$bid.points, deriv=0)$y 
}, df5) 

或者mapply()後面Map()基函數(其非簡化包裝器)

mapply(function(t,t1) predict(t$spline, t1$bid.points, deriv=0)$y, df, df5)