更改数据框中的一些数值 [英] Change some numerical values in data frame

查看:24
本文介绍了更改数据框中的一些数值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据框,其中两个变量(Lat 和 Lon)的值不正确.数据框中不正确的值被列为 999.00,正确的值应该分别是 42.68 和 -72.47.

我想要一种使用 dplyr 替换这些值的简单方法,但我的尝试(见下文)未成功(下面提供了错误).

df$Lat2 <- recode(df$Lat, "999.00"="42.68", .default=x)

<块引用>

lapply(x, f) 中的错误:找不到对象x"

df <- df %>%变异(Lat2 = if_else(Lat == 999.00, 42.68, NULL, NULL))

<块引用>

mutate_impl(.data, dots) 中的错误:评估错误:未使用的参数 (recvLat = 999).

df <- df %>%变异(Lat2 = ifelse(Lat == 999.00, 42.68, NULL))

<块引用>

mutate_impl(.data, dots) 中的错误:评估错误:替换长度为零.另外: 警告信息:在 rep(no, length.out = length(ans)) 中:'x' 为 NULL,因此结果将为 NULL

df <- df %>%变异(Lat2 = case_when(Lat == 999.00 ~ 42.68, TRUE ~ NULL))

<块引用>

mutate_impl(.data, dots) 中的错误:评估错误:下标越界.

对于后三个尝试,如果数字在引号中(即999.00"和42.68"),我会得到相同的错误

解决方案

我们可以将 NULL 放入 list

df %>%变异(Lat2 = ifelse(recvLat == 999.00, 42.68, list(NULL)))# recvLat Lat2#1 999.0 42.68#2 1.5 空#3 2.5 空

代替NULL,它可以是NA

df %>%变异(Lat2 = ifelse(recvLat == 999.00, 42.68, NA_real_))# recvLat Lat2#1 999.0 42.68#2 1.5 北美#3 2.5 北美

如果我们想做相反的事情,只需使用 !=

df %>%变异(Lat2 = ifelse(recvLat != 999.00, 42.68, NA_real_))

根据 OP 的评论,

df %>%变异(Lat2 = ifelse(recvLat == 999.00, 42.68, recvLat))


base R中,我们可以通过创建索引来做到这一点

i1 <- df$recvLat == 999df$recLat[i1] <- 42.68

注意:两种解决方案都有效.

数据

df <- data.frame(recvLat = c(999, 1.5, 2.5))

I have a dataframe with incorrect values for two variables (Lat and Lon). The incorrect values in the dataframe are listed as 999.00, and the correct values should be 42.68 and -72.47, respectively.

I would like an easy way to replace these values using dplyr, but my attempts (see below) have been unsuccessful (errors provided below).

df$Lat2 <- recode(df$Lat, "999.00"="42.68", .default=x)

Error in lapply(x, f) : object 'x' not found

df <- df %>%
mutate(Lat2 = if_else(Lat == 999.00, 42.68, NULL, NULL))

Error in mutate_impl(.data, dots) : Evaluation error: unused argument (recvLat = 999).

df <- df %>%
mutate(Lat2 = ifelse(Lat == 999.00, 42.68, NULL))

Error in mutate_impl(.data, dots) : Evaluation error: replacement has length zero. In addition: Warning message: In rep(no, length.out = length(ans)) : 'x' is NULL so the result will be NULL

df <- df %>%
mutate(Lat2 = case_when(Lat == 999.00 ~ 42.68, TRUE ~ NULL))

Error in mutate_impl(.data, dots) : Evaluation error: subscript out of bounds.

For the latter three attempts, I get the same error if the number are in quotes (i.e. "999.00" and "42.68")

解决方案

We can place NULL in a list

df %>%
    mutate(Lat2 = ifelse(recvLat == 999.00, 42.68, list(NULL)))
#  recvLat  Lat2
#1   999.0 42.68
#2     1.5  NULL
#3     2.5  NULL

Instead of NULL, it can be NA

df %>%
    mutate(Lat2 = ifelse(recvLat == 999.00, 42.68, NA_real_))
#  recvLat  Lat2
#1   999.0 42.68
#2     1.5    NA
#3     2.5    NA

If we want to do the opposite, just use !=

df %>%
    mutate(Lat2 = ifelse(recvLat != 999.00, 42.68, NA_real_))

Based on the OP's comment,

df %>%
    mutate(Lat2 = ifelse(recvLat == 999.00, 42.68, recvLat))


In base R, we can do this by creating an index

i1 <- df$recvLat == 999
df$recLat[i1] <- 42.68

NOTE: Both the solutions work.

data

df <- data.frame(recvLat = c(999, 1.5,  2.5))

这篇关于更改数据框中的一些数值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆