如何有条件地更改R中列的值?

时间:2014-07-18 19:34:33

标签: r

lat         long      class
42.19028    -70.94528  c
42.19028    -72.94528  c
42.19028    -76.94528  b 
42.19028    -77.94528  c
42.19028    -77.94528  c
42.19028    -77.94528  a
42.19028    -77.94528  c
42.19028    -77.94528  d

我想将Class的每个值重命名为c(West)和c(East),具体取决于long是否大于或小于75. 但是只能为CLASS = c更改。例如,如果Class = b或d,则保持原样。

df$class[df$lon>-75] <- "c(west)"
df$class[df$lon<-75] <- "c(east)"

但它不起作用。你能帮忙吗?

错误讯息:

  

警告讯息:在[<-.factor*tmp*,df $ lon&gt; -75,值=   c(NA_integer_,:无效因子水平,NA生成

这可能是因为因素水平不存在。但我不知道如何解决它。

4 个答案:

答案 0 :(得分:3)

df$class <- as.character(df$class)
df$class[df$class == "c"] <- ifelse(
  df$long[df$class=="c"] > (-75),
  "c(west)",
  "c(east)"
)
##

答案 1 :(得分:2)

您可以在使用stringsAsFactors=F时使用read.table。如果class是字符类。

df$class <- c("c(east)", "c(west)")[(df$long>-75) +1]

如果df $ class是factor变量

  levels(df$class) <- c("c(west)", "c(east)")
  df$class <- c("c(east)", "c(west)")[(df$long>-75)+1]

更新

使用新数据集:

  df <- read.table(text="lat    long     class
         42.19028    -70.94528  c
         42.19028    -72.94528  c
         42.19028    -76.94528  b 
         42.19028    -77.94528  c
         42.19028    -77.94528  c
         42.19028    -77.94528  a
         42.19028    -77.94528  c
         42.19028    -77.94528  d",sep="",header=TRUE,stringsAsFactors=F) ####

   df$class[df$class=="c"] <- c("c(east)", "c(west)")[(df$long[df$class=="c"] > -75)+1]
   df$class
   #[1] "c(west)" "c(west)" "b"       "c(east)" "c(east)" "a"       "c(east)"
   #[8] "d"      

答案 2 :(得分:1)

按列名:

df$class <- as.character(df$class)
df[ df$class == "c" & df$long < -75 , 'class' ] <- "c(east)"
df[ df$class == "c" & df$long > -75 , 'class' ] <- "c(west)"

按列号:

df[ df$class == "c" & df$long < -75 , 3] <- "c(east)"
df[ df$class == "c" & df$long > -75 , 3] <- "c(west)"

答案 3 :(得分:1)

以这种方式:

dat <- transform(dat, class = ifelse(class != "c", 
                                     as.character(class), 
                                     c("c(east)", "c(west)")[(long > -75) + 1]))
#        lat      long   class
# 1 42.19028 -70.94528 c(west)
# 2 42.19028 -72.94528 c(west)
# 3 42.19028 -76.94528       b
# 4 42.19028 -77.94528 c(east)
# 5 42.19028 -77.94528 c(east)
# 6 42.19028 -77.94528       a
# 7 42.19028 -77.94528 c(east)
# 8 42.19028 -77.94528       d

其中dat是数据框的名称。