2017-12-30 17 views
2

例データ値:予想通り、それは仕事に縫い目はありませんしかしバイナリ列は

library(data.table) 
setDT(df_stock2) 
df_stock3 <- dcast(melt(df_stock2 , url = 'url')[value != 'NA'], 
     url ~ value, fun.aggregate = length) 

:私はhereからコードを再現してみ

df_stock2 <-data.frame(url= c("https://www.example.com/test","https://www.example2.com/test","https://www.example3.com/test"), stock_yes_01 = c("Google","Microsoft","Yahoo"), stock_yes_02 = c("Yahoo","Google",NA)) 

これがなぜ機能しないのか、何を変更する必要があるのでしょうか?

エラー私が受け取る:

> setDT(df_stock2) 
    Warning message: 
    In melt.data.table(df_stock2, url = "url") : 
     To be consistent with reshape2's melt, id.vars and measure.vars are internally guessed when both are 'NULL'. All non-numeric/integer/logical type columns are conisdered id.vars, which in this case are columns [url, stock_yes_01, stock_yes_02, stock_yes_03, ...]. Consider providing at least one of 'id' or 'measure' vars in future. 
df_stock3 <- dcast(melt(df_stock2, url = 'url')[value != 'NA'], 
    +  url() ~ value, fun.aggregate = length) 
    Error in url() : argument "description" is missing, with no default 
    In addition: Warning message: 
    In melt.data.table(df_stock2, url = "url") : 
     To be consistent with reshape2's melt, id.vars and measure.vars are internally guessed when both are 'NULL'. All non-numeric/integer/logical type columns are conisdered id.vars, which in this case are columns [url, stock_yes_01, stock_yes_02, stock_yes_03, ...]. Consider providing at least one of 'id' or 'measure' vars in future. 

答えて

2

問題がmeltにお電話です。引数idの名前をurlに変更してももちろん動作しません。 id引数は、melt関数に、どの変数を使用して観測値を識別させるべきかを指示します。これを指定しないと、meltは、非数値(または整数または論理)変数を推測してID変数にしようとします。それは警告がすべてであったことです。このエラーは、データを溶かした後にidという名前の列がないことによって発生します。

だから正しい名前id引数を残して、それが動作します:

df_stock3 <- dcast(melt(df_stock2 , id = 'url')[value != 'NA'], 
       url ~ value, fun.aggregate = length) 
+1

私はマイナーな改善を提案したいと思います。 'melt()'は、溶融データから 'NA'値を取り除くために使用できるパラメータ' na.rm'を持っています。したがって、 '[value!= 'NA']'は 'df_stock3 < - dcast(melt(df_stock2、id = 'url'、na.rm = TRUE)、 url_value、fun.aggregate = lengthで保存できます) ' – Uwe