我有一个数据框,其中包含一个名为:“频率”的列。频率有“年”、“周”、“月”等值。现在我想基于频率列创建一个新列,其中年的新对应值为 1,月的对应值为 12,周对应的值为是 48 岁。我尝试为此创建一个函数作为“getValue”,并尝试创建一个新列,在该函数上应用突变(dplyr)。但不幸的是,我收到以下警告,并且所有值都被“1”的值转换。
getValue <- function(input) {
if (input == 'Year')
{
result <- 1
}
else if(input == 'Month')
{
result <- 12
}
else if(input == 'Week')
{
result <- 48
}
return(result)
}
Data = mutate(gateway, YearlyHit = getValue(gateway$Frequency))
Data
这是我收到的警告消息-
Warning message:
In if (input == "Year") { :
the condition has length > 1 and only the first element will be used
如何在 R 中获得所需的结果?