使用自定义功能编辑数据集

时间:2020-01-30 22:48:02

标签: r

使用函数更改我的数据集时遇到麻烦。我想使用一个函数,因为我将不得不使用相同的条件创建多个新列。

首先,我想根据“活动”列中的条件在“数字”列中进行一些更改。然后,我想创建一个名为Freq的新列,该列根据我的Num列中的条件分配值。

我当前的代码似乎未在Num列中进行所需的更改,但确实将更改应用于了新的Freq列。例如,我的函数指定如果Activity ==“ No”则应用0。虽然在运行代码时未进行此更改,但我的Freq列似乎可以识别所做的更改并应用正确的值。此外,“ Frequently”显示在我的Freq列中,其中Num为NA。

我确定我的功能有问题。任何帮助都会很棒!

library(tidyverse)

Activity <- c(rep("NA", 5), rep("No Answer", 5), rep("No", 5), rep("Don't Know", 5), rep("Yes",5))
Num <- c(rep("NA", 5), rep("0", 5), rep("NA", 5), rep("0", 5), 1,2,3,4,5)
data <- data.frame(Activity, Num)


freq_func <- function(Activity, Num) {
  Num <- as.numeric(Num)
  Num <- ifelse(Activity == "No Answer", NA, 
           ifelse(Activity == "No", 0, 
           ifelse(Activity == "Don't Know", NA, Num)))
  ifelse(Num == 0, "None",
         ifelse(Num > 0 & Num < 5, "Rarely", 
         ifelse(Num > 4 & Num <60, "Frequently", 
         ifelse(Num > 60, "Almost Always", NA))))
}

test <- data %>% 
  mutate(Freq = freq_func(Activity, Num))

1 个答案:

答案 0 :(得分:1)

这在考虑@akrun的评论后起作用

library(tidyverse)

Activity <- c(rep("NA", 5), rep("No Answer", 5), rep("No", 5), rep("Don't Know", 5), rep("Yes",5))
Num <- c(rep("NA", 5), rep("0", 5), rep("NA", 5), rep("0", 5), 1,2,3,4,5)
data <- data.frame(Activity, Num)


col_edit <- function(Activity, Num) {
  Num <- as.numeric(as.character(Num))
  ifelse(Activity == "No Answer", NA,
  ifelse(Activity == "No", 0,
  ifelse(Activity == "Don't Know", NA, Activity)))
}


freq_func <- function(Num) {
  ifelse(Num == 0, "None",
         ifelse(Num > 0 & Num < 5, "Rarely", 
         ifelse(Num > 4 & Num <60, "Frequently", 
         ifelse(Num > 60, "Almost Always", NA))))
}

test <- data %>% 
  mutate(Num = col_edit(Activity, Num)) %>% 
  mutate(Freq = freq_func(Num))