在R

时间:2015-07-09 19:51:59

标签: r

数据框为:

> head(td,3)
  CliC_1 CliC_2 CliC_3 Elev Gender PoliticalParty DegreeENV sq_d_shor
1      4      4      4    8      1              1         2  61.65299
2      1      4      1   29      1              3         2  63.60980
3      5      5      5   12      2              1         2 194.13505

前三个变量是因素。

> str(td$CliC_1)
 Factor w/ 5 levels "1","2","3","4",..: 4 1 5 3 5 5 4 5 5 4 ...
> str(td$CliC_2)
 Factor w/ 5 levels "1","2","3","4",..: 4 4 5 4 5 5 5 5 5 4 ...
> str(td$CliC_3)
 Factor w/ 5 levels "1","2","3","4",..: 4 1 5 3 5 5 5 5 4 3 ...

我想要一个能取代等级的功能(" 1"," 2"," 3"," 4" ,. 。)to"非常不同意","不同意",.... 我为此写的函数是:

asign_name<- function(data, var) {
        for (i in 1:nrow(data)) {
                if (var[i] == "1") {i <- "Strongly Disagree"}
                else if (var[i] == "2") {var[i]<- "Disagree"}
                else if (var[i] == "3") {var[i]<- "Neither"}
                else if (var[i] == "4") {var[i]<- "Agree"}
                else if (var[i] == "5") {var[i]<- "Strongly Agree"}

        }

        return (data)
}

但它没有改变价值观:

> head(asign_name(td, td$CliC_1), 3)
  CliC_1 CliC_2 CliC_3 Elev Gender PoliticalParty DegreeENV sq_d_shor
1      4      4      4    8      1              1         2  61.65299
2      1      4      1   29      1              3         2  63.60980
3      5      5      5   12      2              1         2 194.13505

我收到警告信息: 警告信息:

1: In `[<-.factor`(`*tmp*`, i, value = "Agree") :
  invalid factor level, NA generated

2 个答案:

答案 0 :(得分:3)

我可能会使用levels<-命令来替换因子级别。这是一个例子

# define mapping
rr<-list(
  "Strongly Disagree"="1",
  "Disagree"="2",
  "Neither"="3",
  "Agree"="4",
  "Strongly Agree"="5"
)

# sample data
dd<-data.frame(
    a=factor(sample(1:5, 20, replace=T)),
    b=factor(sample(1:5, 20, replace=T))
)

# helper function
changelevels<-function(x) {
    levels(x)<-rr
      x
}

# column replacement
dd[,1:2]<-lapply(dd[,1:2], changelevels)

答案 1 :(得分:2)

您可以创建一个键/值向量,并用它来替换前3列中的值。

 v1 <- setNames(c('Strongly Disagree', 'Disagree', 'Neither', 
             'Agree', 'Strongly Agree'),1:5)
 td[1:3] <- lapply(td[1:3], function(x) factor(v1[as.character(x)], levels=v1))
 td
 #            CliC_1         CliC_2            CliC_3 Elev Gender PoliticalParty
 #1             Agree          Agree             Agree    8      1              1
 #2 Strongly Disagree          Agree Strongly Disagree   29      1              3
 #3    Strongly Agree Strongly Agree    Strongly Agree   12      2              1
 #  DegreeENV sq_d_shor
 #1         2  61.65299
 #2         2  63.60980
 #3         2 194.13505