根据R

时间:2018-02-22 16:54:14

标签: r grepl

考虑数据集:

activity <- c("play football", "basketball player", "guitar sono","cinema", "piano")
country_and_type <- c("uk", "uk", "spain", "uk", "uk")
dataset <- data.frame(activity, country_and_type)

|activity         |country_and_type   |
|play football    |uk                 |
|basketball playe |uk                 |
|guitar sono      |spain              |
|cinema           |uk                 |
|piano            |uk                 |

以及这些清单:

sport <- ("football", "basketball", "handball", "baseball")
music <- ("guitar", "piano", "microphone")

如果初始数据集$ country_and_type值为&#34; uk&#34; ,我的目标是根据字符串匹配在数据集$ country_and_type列的括号中添加列表名称。 如果没有匹配的值,则类型应为&#34;其他&#34;。

更清楚一点,这是预期的输出:

|activity         |country_and_type   |
|play football    |uk (sport)         |
|basketball playe |uk (sport)         |
|guitar sono      |spain              |
|cinema           |uk  (other)        |
|piano            |uk (music)         |

你对如何制作它有所了解吗?

1 个答案:

答案 0 :(得分:1)

dataset$type=NA
> dataset$type[grepl(paste(sport,collapse = "|"),a)]="sport"
> dataset$type[grepl(paste(music,collapse = "|"),a)]="music"
> dataset
                      a  type
1         play football sport
2     basketball player sport
3           guitar sono music
4          french piano music
5           ok handball sport
6         baseball game sport
7 microphone for singer music
>

版本之后:

> sp=grepl(paste(sport,".*uk",collapse = "|"),do.call(paste,dataset))
> ms=grepl(paste(music,".*uk",collapse = "|"),do.call(paste,dataset))
> uk=grepl("uk",do.call(paste,dataset))
> dataset$type=""
> dataset$type[sp]="(sport)"
> dataset$type[ms]="(music)"
> dataset$type[!(ms|sp)&uk]="(other)"
> transform(dataset,country_and_type=paste(country_and_type,type))[-3]
           activity country_and_type
1     play football       uk (sport)
2 basketball player       uk (sport)
3       guitar sono           spain 
4            cinema       uk (other)
5             piano       uk (music)