list 从列表转换为数字
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43599347/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert from list to numeric
提问by Tim
Hi I've had a look at a lot of other qns/answers and haven't been able to resolve coercing from list form to numeric form. If it is of use, the list is originally drawn from a factor (and is 1x33 rows).
嗨,我看过很多其他的 qns/答案,但无法解决从列表形式到数字形式的强制转换问题。如果它有用,该列表最初是从一个因子中提取的(并且是 1x33 行)。
My list is defined by:
我的列表定义为:
tmpseqsf[[1]]
which provides:
它提供:
TradeValue
1 72914431
2 25325
3 20139
4 ...
So based on other advice (Stackoverflow, etc) I use:
因此,根据其他建议(Stackoverflow 等),我使用:
tmpx <-as.numeric(tmpseqsf[[1]])
but I get the error:
但我收到错误:
Error: (list) object cannot be coerced to type 'double'
And just confirming nothing has happened:
只是确认没有发生任何事情:
is.numeric(tmpseqsf[[1]])
[1] FALSE
Is there something I am completely missing?
有什么我完全想念的吗?
回答by ahly
If the values are of type factor, then you should convert them by as.numeric(as.character(unlist(tmpseqsf[[1]])))
如果值是因子类型,那么您应该将它们转换为 as.numeric(as.character(unlist(tmpseqsf[[1]])))