从不会列出的另一列中提取数据

时间:2019-09-25 07:27:11

标签: r

        Order ID          Country Region   |   Topreg
-------------------------|-----------------|--------------
CA-2014-AB10015140-41954 |  United States  | Central US
IN-2014-JR162107-41675   |    Australia    |   Oceania
IN-2014-CR127307-41929   |    Australia    |   Oceania
ES-2014-KM1637548-41667  |    Germany      |   Western Europe
SG-2014-RH9495111-41948  |    Senegal      |   Western Africa
IN-2014-JM156557-41818   |    Australia    |   Oceania
IN-2012-TS2134092-41219  |    New Zealand  |   Oceania
IN-2013-MB1808592-41378  |    New Zealand  |   Oceania
CA-2014-AB10015140-41954 |  United States  | Central US
CA-2012-AB10015140-40974 |  United States  | Western US
CA-2012-AB10015140-40974 |  United States  | Western US
ID-2013-AJ107801-41383   |    Afghanistan  |   Southern Asia

大家好,我有这个数据,我想从“地区”列中提取“欧洲”或“亚洲”。我在下面编写了一个函数,并对名为“ topreg”的新列做了一些粗略的介绍,但出现了所列的错误。有人可以帮助我告诉我出了什么问题吗?新列“ topreg”仅包含“其他”。

funcreg <- function(x){     
  if (grepl("Europe", x)){
  y <- c("Europe")} 
  else if (grepl("Asia", x)) {
    y <- c("Asia")} 
  else {y <- c("Others")}
}

df["topreg"] <- lapply(df$Region, funcreg)
error message:
Warning message:
In `[<-.data.frame`(`*tmp*`, "topreg", value = list("Others", "Others",  :
  provided 51290 variables to replace 1 variables

0 个答案:

没有答案