首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >文本值中的二进制列

问文本值中的二进制列
EN

Stack Overflow用户
提问于 2017-12-31 04:53:05
回答 1查看 64关注 0票数 2

示例数据:

代码语言:javascript
复制
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))

我尝试从here中重现代码

代码语言:javascript
复制
library(data.table)
setDT(df_stock2 )
df_stock3 <- dcast(melt(df_stock2 , url = 'url')[value != 'NA'],
      url ~ value, fun.aggregate = length)

然而,它并不能像预期的那样工作。

你知道为什么这不起作用吗?或者我必须改变什么?

我收到的错误:

代码语言:javascript
复制
> 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.
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-12-31 22:03:14

问题在于您对melt的调用。您已经将参数id重命名为url,这当然不起作用。id参数告诉melt函数,应该使用哪些变量来标识观测值。如果您不指定它,那么melt将尝试猜测并接受任何非数字(或整数或逻辑)变量作为id变量。这就是警告的全部内容。然后,错误是由于在熔化数据之后没有任何名为id的列而导致的。

因此,只需正确命名id参数,它就能正常工作:

代码语言:javascript
复制
df_stock3 <- dcast(melt(df_stock2 , id = 'url')[value != 'NA'],
               url ~ value, fun.aggregate = length)
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48037794

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档