How can i convert a factor column that contains decimal numbers to numeric?

梦想的初衷 提交于 2019-11-29 04:36:11
as.numeric(as.character(sub("," , ".", SOLK$Close)))

I noticed (after you posted a better example) that you may need to do some conversion on the "Time" values as well:

> SOLK$Close.n <- as.numeric(sub("," , ".", SOLK$Close))
> head(SOLK)
        Time Close Volume Close.n
1 10:27:03,6  0,99   1000    0.99
2 10:32:58,4  0,98    100    0.98
3 10:34:16,9  0,98    600    0.98
4 10:35:46,0  0,97    500    0.97
5 10:35:50,6  0,96     50    0.96
6 10:35:50,6  0,96   1000    0.96

Since that is also a factor, you will gain generality if you finish the conversions. Perhaps:

SOLK$Time.n <- as.POSIXct(sub("," , ".", SOLK$Time), format="%H:%M:%S")

I think your numbers have commas rather than periods, so you might call read.table with dec=",".

Carl Witthoft
as.numeric(as.character(SOLK$Close))

This is in the R-FAQ, 7.10 .

SOLK<-read.table('Book1.csv',header=TRUE,sep=';', colClasses = "character")
SOLK[, position] <- as.numeric(SOLK[, position])
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!