簡體   English   中英

將非數字行和列轉換為零

[英]Convert non-numeric rows and columns to zero

我有一個r包中的數據,其中X是包含所有數據的數據集

library(ISLR)
data("Hitters")
X=Hitters
head(X)

這是數據的一部分:

                 AtBat Hits HmRun Runs RBI Walks Years CAtBat CHits CHmRun CRuns CRBI CWalks League Division PutOuts Assists Errors Salary NewLeague
-Andy Allanson      293   66     1   30  29    14     1    293    66      1    30   29     14      A        E     446      33     20     NA         A
-Alan Ashby         315   81     7   24  38    39    14   3449   835     69   321  414    375      N        W     632      43     10  475.0         N
-Alvin Davis        479  130    18   66  72    76     3   1624   457     63   224  266    263      A        W     880      82     14  480.0         A
-Andre Dawson       496  141    20   65  78    37    11   5628  1575    225   828  838    354      N        E     200      11      3  500.0         N
-Andres Galarraga   321   87    10   39  42    30     2    396   101     12    48   46     33      N        E     805      40      4   91.5         N
-Alfredo Griffin    594  169     4   74  51    35    11   4408  1133     19   501  336    194      A        W     282     421     25  750.0         A

我想將非數值的所有列和行都轉換為零,是否有任何簡單的方法可以做到這一點? 我在這里找到了一個示例 ,該示例僅介紹如何刪除一列的行,但對於更多列,我必須手動為每一列進行刪除。

r是否對所有列和行都執行此功能?

要刪除非數字列,也許是這樣的?

df %>%
    select(which(sapply(., is.numeric)))
#                  AtBat Hits HmRun Runs RBI Walks Years CAtBat CHits CHmRun
#-Andy Allanson      293   66     1   30  29    14     1    293    66      1
#-Alan Ashby         315   81     7   24  38    39    14   3449   835     69
#-Alvin Davis        479  130    18   66  72    76     3   1624   457     63
#-Andre Dawson       496  141    20   65  78    37    11   5628  1575    225
#-Andres Galarraga   321   87    10   39  42    30     2    396   101     12
#-Alfredo Griffin    594  169     4   74  51    35    11   4408  1133     19
#                  CRuns CRBI CWalks PutOuts Assists Errors Salary
#-Andy Allanson       30   29     14     446      33     20     NA
#-Alan Ashby         321  414    375     632      43     10  475.0
#-Alvin Davis        224  266    263     880      82     14  480.0
#-Andre Dawson       828  838    354     200      11      3  500.0
#-Andres Galarraga    48   46     33     805      40      4   91.5
#-Alfredo Griffin    501  336    194     282     421     25  750.0

要么

df %>%
    select(-which(sapply(., function(x) is.character(x) | is.factor(x))))

或更整潔(由於@AntoniosK):

df %>% select_if(is.numeric)

更新資料

要另外用0替換NA ,您可以執行

df %>% select_if(is.numeric) %>% replace(is.na(.), 0)
#                  AtBat Hits HmRun Runs RBI Walks Years CAtBat CHits CHmRun
#-Andy Allanson      293   66     1   30  29    14     1    293    66      1
#-Alan Ashby         315   81     7   24  38    39    14   3449   835     69
#-Alvin Davis        479  130    18   66  72    76     3   1624   457     63
#-Andre Dawson       496  141    20   65  78    37    11   5628  1575    225
#-Andres Galarraga   321   87    10   39  42    30     2    396   101     12
#-Alfredo Griffin    594  169     4   74  51    35    11   4408  1133     19
#                  CRuns CRBI CWalks PutOuts Assists Errors Salary
#-Andy Allanson       30   29     14     446      33     20    0.0
#-Alan Ashby         321  414    375     632      43     10  475.0
#-Alvin Davis        224  266    263     880      82     14  480.0
#-Andre Dawson       828  838    354     200      11      3  500.0
#-Andres Galarraga    48   46     33     805      40      4   91.5
#-Alfredo Griffin    501  336    194     282     421     25  750.0 

您可以使用sapply/inherits獲得數字列。

X <- Hitters
inx <- sapply(X, inherits, c("integer", "numeric"))
Y <- X[inx]

然后,刪除具有非數字條目的行沒有多大意義,因為它們已經被刪除了,但是您可以

inx <- apply(Y, 1, function(y) all(inherits(y, c("integer", "numeric"))))
Y[inx, ]
library(ISLR)
data("Hitters")
d = head(Hitters)

library(dplyr)

d %>% 
  mutate_if(function(x) !is.numeric(x), function(x) 0) %>%   # if column is non numeric add zeros
  mutate_all(function(x) ifelse(is.na(x), 0, x))             # if there is an NA element replace it with 0

#   AtBat Hits HmRun Runs RBI Walks Years CAtBat CHits CHmRun CRuns CRBI CWalks League Division PutOuts Assists Errors Salary NewLeague
# 1   293   66     1   30  29    14     1    293    66      1    30   29     14      0        0     446      33     20    0.0         0
# 2   315   81     7   24  38    39    14   3449   835     69   321  414    375      0        0     632      43     10  475.0         0
# 3   479  130    18   66  72    76     3   1624   457     63   224  266    263      0        0     880      82     14  480.0         0
# 4   496  141    20   65  78    37    11   5628  1575    225   828  838    354      0        0     200      11      3  500.0         0
# 5   321   87    10   39  42    30     2    396   101     12    48   46     33      0        0     805      40      4   91.5         0
# 6   594  169     4   74  51    35    11   4408  1133     19   501  336    194      0        0     282     421     25  750.0         0

如果要避免使用function(x) ,可以使用此功能

d %>% 
  mutate_if(Negate(is.numeric), ~0) %>%  
  mutate_all(~ifelse(is.na(.), 0, .)) 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM