2016-12-15 88 views
1

我有一個名爲「遊戲」的數值框,其中有幾列數字。原始的csv文件有一些缺失的值,當我讀取它們時,它變成了NAs。我試圖用行中值(已經存儲爲數據幀的列)替換這些NAs。我無法將原始的NA從角色強制轉換爲數字。R - 無法將數據框中的NAs更改爲數字

我首先找到了缺失值的索引。

ng <- which(is.na(games), arr.ind = TRUE) 

然後我嘗試用列「linemedian」中的值替換NA。

games[ng] <- games[ng[,1], "linemedian"] 
games[ng] 
[1] " -3.25" " 9.98" " -9.1" " -9.1" " 14.0" " -3.25" " 9.98" " -3.25" " 9.98" " 2.30" " 13.75" "-24.00" " 3.71" " 15.94" " 14.25" " -9.83" " 13.75" " -4.88" 

用任意數字替換NA也不起作用。

games[is.na(games)] <- 0 
[1] " 0.0" " 0.0" " 0" " 0" " 0" " 0.0" " 0.0" " 0.0" " 0.0" " 0.00" " 0.00" " 0.00" " 0" " 0" " 0.00" " 0.00" " 0.00" " 0.00" 

我認爲刪除空格可能會改變結果,但它沒有。

games[ng] <- as.numeric(trimws(games[ng[,1], "linemedian"])) 
[1] "-3.25" "9.98" "-9.1" "-9.1" "14" "-3.25" "9.98" "-3.25" "9.98" "2.3" "13.75" "-24" "3.71" "15.94" "14.25" "-9.83" "13.75" "-4.88" 

,沒有工作的其他嘗試:

games[ng] <- type.convert(games[ng]) # using type.convert() 

games[, -c(1,2)] <- as.numeric(games[, -c(1,2)]) # first two columns are metadata 
Error: (list) object cannot be coerced to type 'double' 

games[, -c(1,2)] <- as.numeric(unlist(games[, -c(1,2)]))  

games[ng] <- as.numeric(as.character(trimws(games[ng[,1], "linemedian"]))) 

# New Addition from Answer 
games[, sapply(games, is.numeric)][ng] <- games[, sapply(games, is.numeric)][ng[,1], "linemedian"] 

我知道肯定是我分配給遊戲[NG]的值是一個數字。

games[ng[,1], "linemedian"] 
[1] -3.25 9.98 -9.10 -9.10 14.00 -3.25 9.98 -3.25 9.98 2.30 13.75 -24.00 3.71 15.94 14.25 -9.83 13.75 -4.88 
typeof(games[ng[,1], "linemedian"]) 
[1] "double" 

我到處都看的堆棧溢出板,答案顯然應該是遊戲[is.na(遊戲)] < - 值。但那不起作用。任何人有一些想法?

下面是完整的代碼,如果要複製:

## Download Raw Files 

download.file("http://www.thepredictiontracker.com/ncaa2016.csv", 
      "data/ncaa2016.csv") 

download.file("http://www.thepredictiontracker.com/ncaapredictions.csv", 
      "data/ncaapredictions.csv") 

## Create Training and Prediction Data Sets 

games <- read.csv("data/ncaa2016.csv", header = TRUE, stringsAsFactors = FALSE, 
       colClasses=c(rep("character",2),rep("numeric",72))) 

preds <- read.csv("data/ncaapredictions.csv", header = TRUE, stringsAsFactors = TRUE) 
colnames(preds)[colnames(preds) == "linebillings"] <- "linebill" 
colnames(preds)[colnames(preds) == "linebillings2"] <- "linebill2" 
colnames(preds)[colnames(preds) == "home"] <- "Home" 
colnames(preds)[colnames(preds) == "road"] <- "Road" 

## Remove Columns with too many missing values 

rm <- unique(c(names(games[, sapply(games, function(z) sum(is.na(z))) > 50]), # Games and predictions 
      names(preds[, sapply(preds, function(z) sum(is.na(z))) > 10]))) # with missing data 

games <- games[, !(names(games) %in% rm)] # Remove games with no prediction data 

preds <- preds[, !(names(preds) %in% rm)] # Remove predictions with no game data 

## Replace NAs with Prediction Median 
ng <- which(is.na(games), arr.ind = TRUE) 
games[ng] <- games[ng[,1], "linemedian"] 

而且,我不能發佈整個dput()輸出,但這裏是一個有點數據集只是爲了顯示結構。

dput(head(games[1:6])) 

structure(list(Home = c("Alabama", "Arizona", "Arkansas", "Arkansas St.", 
"Auburn", "Boston College"), Road = c("USC", "BYU", "Louisiana Tech", 
"Toledo", "Clemson", "Georgia Tech"), line = c("12", "-2", "24.5", 
"4", "-8.5", "-3"), linesag = c(12.19, 0.97, 24.26, -2.07, -4.78, 
-2.74), linepayne = c(12, -0.81, 12.53, -0.86, -10.72, -3.87), 
linemassey = c(19.15, -2.1, 21.07, -8.68, -5.45, -6.76)), .Names = c("Home", 
"Road", "line", "linesag", "linepayne", "linemassey"), row.names = c(NA, 
6L), class = "data.frame") 

最後,我在x86_64-w64-mingw32上運行R版本3.2.1。

+2

您需要在'games'的適當子集中調用'dput'的結果,以便人們可以看到數據結構是什麼。 – alistaire

+0

我認爲我們都誤解這個輸出是表示'遊戲'中的值是字符。我會發布一些調試代碼。 –

回答

1

沒有測試用例,這將是未經測試的。看來你得到一個全局替換,而是因爲你的一些列的是性格,你強迫從0強制所有字符值我可能試圖限制,只顯示數字列:

games[ , sapply(games, is.numeric) ][ ng ] <- 
         games[ , sapply(games, is.numeric)][ng[,1], "linemedian"] 

修改後的幾乎可重複的代碼,我已經得出結論,原來的代碼是成功的,但你的檢查的產量問題區域>

str(games[ , sapply(games, is.numeric)][ng[,1], "linemedian"]) 
#num [1:23] -3.25 9.98 -9.1 -9.1 14 -3.25 9.98 -3.25 9.98 2.3 ... 

games[ ng ] <- 
         games[ , sapply(games, is.numeric)][ng[,1], "linemedian"] 
games[ ng[1:2,] ] 
[1] " -3.25" " 9.98" 

> ng[1:2,] 
    row col 
[1,] 619 3 
[2,] 678 3 

> str(games) 
'data.frame': 720 obs. of 58 variables: 
$ Home   : chr "Alabama" "Arizona" "Arkansas" "Arkansas St." ... 
$ Road   : chr "USC" "BYU" "Louisiana Tech" "Toledo" ... 
$ line   : num 12 -2 24.5 4 -8.5 -3 8.5 37 -10.5 5 ... 
$ linesag  : num 12.19 0.97 24.26 -2.07 -4.78 ... 
$ linepayne : num 12 -0.81 12.53 -0.86 -10.72 ... 
deleted 

> games[ c(619,678) , 3] 
#[1] -3.25 9.98 
> games[ matrix(c(619,678,3,3), ncol=2)] 
[1] " -3.25" " 9.98" 

所以第三列在轉讓之後仍數字,但對於原因,我不明白矩陣索引提取的打印功能的輸出看起來像是它在fa時的字符ct數字。

+0

我確實嘗試過,但沒有奏效。我最初的嘗試是在數字列上調用as.numeric(除前兩個外)。無論如何,我將它添加到我無法使用的嘗試列表中。 –

+0

同意。我作爲猜測提供的代碼...沒有工作。但是我們都誤解了'遊戲[ng]'的輸出。所以我認爲你的代碼實際上一直在工作,並且我的代碼也工作,當它簡化爲:'games [ng] < - games [,sapply(games,is.numeric)] [ng [,1],「linemedian」] ' –

+0

我證實了這一點,你說得對。在給NAs分配值之前,我用'colSums(遊戲[,c(3,26)],na.rm = TRUE)'作爲列總和。在分配後,我又拿了一個列總和,並且值改變了。驗證算法是很容易驗證的。謝謝您的幫助。 –

相關問題