根据前一列中的整数在 R 中添加字符因子列?

问题描述 投票:0回答:1

我有一个冰雹事件及其数字相关损失的数据框。我想添加一个具有“是”或“否”因素的列 (DAMAGE_PROP),以指示现有列 DAMAGE_PROPERTY_NUM 中指示的财产是否受到损坏。 使用我当前使用的代码,我收到“未使用的参数”错误(级别= 2,标签= c(“是”,“否”))。

你能帮忙吗? 这是代码片段,以及列出的损失表:

 AZ_HAIL$DAMAGE_PROP <- as.factor(AZ_HAIL$DAMAGE_PROPERTY_NUM, 
                              levels = 2, 
                              labels = c("yes", "no"))

    > table(AZ_HAIL$DAMAGE_PROPERTY_NUM)

       $0    $250   $400   $500  $1000  $2000   $5000    $6000    $10000   $15000    $20000 etc.
     1163       2      1      5      5      4      12        1         7        1         3 



I also tried the more general code 

factor(x = character(), levels, labels = levels,
       exclude = NA, ordered = is.ordered(x), nmax = NA)

not using "as.factor"

    > AZ_HAIL$DAMAGE_PROP <- factor(AZ_HAIL$DAMAGE_PROPERTY_NUM = character(), 
    Error: unexpected '=' in "AZ_HAIL$DAMAGE_PROP <- factor(AZ_HAIL$DAMAGE_PROPERTY_NUM ="

I have also tried 
    answer <- factor(c("yes", "no"))
    type <- unlist(lapply(answer, function(x) ifelse(as.numeric(substr(as.character(x), 2,   nchar(as.character(x))))>0, 'yes', 'no')))
    damage <- data.frame(answer=answer, type=factor(type))
    is.factor(AZ_HAIL$DAMAGE_PROP)
    damage

    results:
      answer type
    1    yes <NA>
    2     no <NA>
r label factors
1个回答
0
投票

您可以说

DAMAGE_PROPERTY_NUM > 0
,结果是
FALSE
/
TRUE
,然后给出
labels=
"no"
"yes"
,它将按字母顺序应用。

> AZ_HAIL |>
+   transform(DAMAGE_PROP=factor(DAMAGE_PROPERTY_NUM > 0, labels=c('no', 'yes')))
    DAMAGE_PROPERTY_NUM DAMAGE_PROP
1                     0          no
2                     0          no
...
16                    0          no
17                10000         yes
18                    0          no
19                    0          no
20                    0          no
21                    0          no
22                    0          no
23                  500         yes
24                    0          no
25                    0          no
26                    0          no
27                    0          no
28                    0          no
29                    0          no
...

数据:

set.seed(42)
AZ_HAIL <- data.frame(
  DAMAGE_PROPERTY_NUM=sample(c(0, 250, 400, 500, 1000, 2000, 5000, 6000, 10000, 
                               15000, 20000), 1500,
       prob=c(1163, 2, 1, 5, 5, 4, 12, 1, 7, 1, 3), replace=TRUE))
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.