2

example data:

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))

I try to reproduce the code from here:

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

However it doesn't seam to work as expected.

Any idea why this is not working or what I have to change?

the error I receive:

> 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.
0

1 Answer 1

2

The problem is your call to melt. You've rename the argument id to url, and that of course does not work. The id argument tells the melt function, which variables should be used to identify observations. If you don't specify it, then melt will try to guess and take any non-numeric (or integer or logical) variables to be the id variables. That's what the warning was all about. The error is then caused by there not being any column named id after melting the data.

So just leave the id argument named correctly, and it works:

df_stock3 <- dcast(melt(df_stock2 , id = 'url')[value != 'NA'],
               url ~ value, fun.aggregate = length)
Sign up to request clarification or add additional context in comments.

1 Comment

I would like to suggest a minor improvement. melt() has a parameter na.rm which can be used to remove NA values from the molten data. So, [value != 'NA'] can be saved by df_stock3 <- dcast(melt(df_stock2 , id = 'url', na.rm = TRUE), url ~ value, fun.aggregate = length)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.