替换所有以独立于类型的变量属性为条件的负值

Replacing all negative values conditional on the attributes of the variable independent of type

我有一个非常大的混合数据集(字符变量、数值变量、因子),其中负值通常表示缺失值,请参阅 Scales,但并非总是如此,请参阅 Profit :

     Country Ccode  Year Profit Scale    ID Happiness_d Power_d  ID_d
  <chr>   <fcr> <dbl>     <dbl> <labelled>    <dbl>    <dbl>   <dbl>  <dbl>
1 France  FR     2000      1000  NA        1      40000. 160000.  1.67
2 France  FR     2001     -1200   1        1      80000. 320000.  1.67
3 France  FR     2000      1400   0        2      40000. 160000.  1.67
4 France  FR     2001      1600   3        2      80000. 320000.  1.67
5 UK      UK     2000     -1000  -9        3      40000. 160000.  1.67
6 UK      UK     2001      1000   2        3      80000. 320000.  1.67
7 UK      UK     2000      1000   4        4      40000. 160000.  1.67
8 UK      UK     2001      1000   0        4      80000. 320000.  1.67

我想使用 NA 替换所有负值:

df[df< 0] <- NA

问题是,虽然它旨在删除代表 NA 的负值,例如 Scale,但它会在示例数据集中删除 Profit 中的负数,这显然不是 NA .

因此,我想让结果以变量的范围为条件。 Scale变量的结构如下:

Class 'labelled'  atomic [1:135894] NA NA 2 NA NA NA NA NA NA NA ...
  ..- attr(*, "label")= chr "Do You Use Technology Licensed From A Foreign-Owned Company?"
  ..- attr(*, "format.stata")= chr "%24.0g"
  ..- attr(*, "labels")= Named num [1:3] -9 1 2
  .. ..- attr(*, "names")= chr [1:3] "Don't Know (Spontaneous)" "Yes" "No"
> names(New_Comprehensive_June_25_2018$e6)

我发现使用 havenlink 您可以从中得出因子水平;

   ..- attr(*, "labels")= Named num [1:3] -9 1 2

与 get_values()。

get_values(df$Scale)
[1] -9 1 2

是否可以让解决方案只去除这些负面因素而不是其他负面价值?

..- attr(*, "labels")= Named num [1:3] -9 1 2

明确地说,所需的输出为:

  Country Ccode  Year Profit Scale    ID Happiness_d Power_d  ID_d
  <chr>   <fcr> <dbl>     <dbl> <dbl>    <dbl>    <dbl>   <dbl>  <dbl>
1 France  FR     2000      1000  NA        1      40000. 160000.  1.67
2 France  FR     2001     -1200   1        1      80000. 320000.  1.67
3 France  FR     2000      1400   0        2      40000. 160000.  1.67
4 France  FR     2001      1600   3        2      80000. 320000.  1.67
5 UK      UK     2000     -1000  **NA**    3      40000. 160000.  1.67
6 UK      UK     2001      1000   2        3      80000. 320000.  1.67
7 UK      UK     2000      1000   4        4      40000. 160000.  1.67
8 UK      UK     2001      1000   0        4      80000. 320000.  1.67

dput 示例(请注意变量 Scale 实际上并不存在:

h7a = structure(c(1, -9, 2, 3, 1, 3, -9, 2, 3, 1, 2, 1, 3, 
    3, 2, 2, 1, 2, 2, 1, 2, -9, 1, 4, 3, 3, 1, 1, 1, 1, 3, 4, 
    3, 1, 2, 2, 1, 2, 1, NA, 2, 1, 2, 4, 3, 1, 3, 4, 4, 3, 2, 
    4, 1, 1, 2, 3, 2, 2, 2, 2, 1, 2, 1, 3, 4, 3, 1, 3, 1, 2, 
    3, 3, 3, 1, 1, 4, -9, 4, 3, 1, 2, 3, 1, -9, 1, 4, 1, 3, 1, 
    -9, 1, 1, 1, 1, 2, 3, 1, 3, 1, 2, 1, 2, 3, 4, 3, 3, 2, 4, 
    3, 3, 1, -9, 1, -7, 3, 1, 1, 2, 1, 2, -7, 2, 3, 1, 3, -7, 
    3, 4, 3, 2, 3, NA, 3, 3, 3, 1, 1, 2, 2, -9, 3, 1, 1, 2, 1, 
    1, -9, -9, -9, 2, -9, 1, 2, 3, 2, 3, 3, 3, 3, 1, 2, -9, 4, 
    3, 3, 1, 2, 2, 4, 4, 4, 3, 2, 1, 2, 2, 2, 2, 2, 3, 2, 2, 
    -9, 4, 4, 4, 2, 1, -7, 2, 2, 1, 1, 2, 1, 2, 2, 4, 2, 3, -7, 
    3, 3, 3, 4, 2, 4, 2, NA, 1, 3, 1, 2, 3, 4, 3, -9, 3, 3, 4, 
    3, 2, 4, 1, 3, 1, 3, 4, 3, 1, 3, 3, 3, NA, 1, 3, 3, -7, 1, 
    1, 3, 2, 1, 4), label = "The Court System Is Fair, Impartial And Uncorrupted", format.stata = "%24.0g", class = "labelled", labels = structure(c(NA, 
    NA, 1, 2, 3, 4), .Names = c("Don't Know (Spontaneous)", "Does Not Apply", 
    "Strongly disagree", "Tend to disagree", "Tend to agree", 
    "Strongly agree"))),

这是一个您可以应用于您的数据集的简单示例。

# example data
df = data.frame(a = c("A","A","B"),
                x = c(1,2,3),
                y = c(NA,3,-7),
                z = c(200,300,-400))

library(dplyr)

df %>% mutate_if(is.numeric, ~ifelse(between(min(., na.rm = T), -9, -1) & .<0, NA, .))

#   a x  y    z
# 1 A 1 NA  200
# 2 A 2  3  300
# 3 B 3 NA -400

您可以更新 (mutate) 仅当该列为数字且该列的最小值介于 -9 和 -1 之间时。更新是用 NA.

替换负值

这假设您只有整数值。如果没有,您可以使用 between(..., -9, 0).

Base-R 解决方案:

# Find negative value from 3rd column onwards, replace it with NA 
# and bind with Country,Ccode and Profit columns. 
cbind(df[,c(1,2,4)],do.call(cbind, lapply(df[,-c(1,2,4)], function(x) ifelse(x<0,NA,x))))

输出:

     Country Ccode Profit Year Scale ID Happiness_d Power_d ID_d
  1  France    FR   1000 2000    NA  1       40000  160000 1.67
  2  France    FR  -1200 2001     1  1       80000  320000 1.67
  3  France    FR   1400 2000     0  2       40000  160000 1.67
  4  France    FR   1600 2001     3  2       80000  320000 1.67
  5      UK    UK  -1000 2000    NA  3       40000  160000 1.67
  6      UK    UK   1000 2001     2  3       80000  320000 1.67
  7      UK    UK   1000 2000     4  4       40000  160000 1.67
  8      UK    UK   1000 2001     0  4       80000  320000 1.67