adis_sep%>%rowwise()%>%
mutate(new=c(V1,V2,V3)[grep("@",c(V1,V2,V3))[1]])
Source: local data frame [4 x 4]
Groups: <by row>
# A tibble: 4 x 4
V1 V2 V3 new
<chr> <chr> <chr> <chr>
1 1 2 3 NA
2 asdf@com.com 5 6 asdf@com.com
3 7 sdfg@com.com 9 sdfg@com.com
4 qer@f.com 11 12 qer@f.com
Если в более чем одном столбце есть @
, выбирается первый.Кроме того, будьте осторожны при использовании as.data.frame()
и добавьте stringsAsFactors = FALSE
, иначе этот код не будет работать.
edit
Для второго случая
adis_sep%>%rowwise()%>%
mutate(new=c(Organisation,Kontaktperson,Mail,sonst1,sonst2)[grep("@",c(Organisation,Kontaktperson,Mail,sonst1,sonst2))[1]])
Source: local data frame [2 x 6]
Groups: <by row>
# A tibble: 2 x 6
Organisation Kontaktperson Mail sonst1 sonst2 new
<chr> <chr> <chr> <chr> <chr> <chr>
1 10 Jahre xx Familienferienwochen " x y" " adf@xx.ch" NA NA " adf@xx.ch"
2 50plus talk " adf adf" " führerin " " info@asdf.ch" NA " info@asdf.ch"