我想將字符列從數據框轉換爲數字。但是,我得到的結果是四捨五入的值。as.numeric正在舍入值
無論我通過研究其他同樣性質的問題而嘗試過的東西,都不適合我。我檢查了我想要轉換的列向量的類,它是一個字符,而不是一個因素。
這裏是我的代碼片段:
some_data <- read.csv("file.csv", nrows = 100, colClasses = c("factor", "factor", "character", "character"))
y <- Vectorize(function(x) gsub("[^\\.\\d]", "", x, perl = TRUE))
some_data$colC <- y(data1$colC)
data1$colD <- y(data1$colCD)
data1$colC <- as.numeric(data1$colC)
data1$colD <- as.numeric(data1$colD)
編輯:
> dput(head(data1))
structure(list(colA = structure(c(2L, 2L, 5L, 6L, 5L, 6L), .Label = c("(Other)",
"Direct", "Display", "Email", "Organic Search", "Paid Search",
"Referral", "Social Network"), class = "factor"), colB = structure(c(1L,
2L, 2L, 2L, 1L, 1L), .Label = c("No", "Yes"), class = "factor"),
colC = c("4023107.87", "3180863.42", "2558777.81", "2393736.25",
"1333148.48", "1275627.13"), colD = c("49731596.35", "33604210.26",
"20807573.12", "20061467.30", "10488358.77", "10442249.09"
)), .Names = c("colA", "colB", "colC", "colD"), row.names = c(NA,
6L), class = "data.frame")
我試圖重現你的問題,但我沒有'file.csv' – cory
你能提供一個可重複的例子嗎?描述中的read.csv()不提供關於您的數據的任何信息 – fhlgood
請嘗試使您的示例具有可重現性。也許只是顯示'dput(head(data1))' – Frank