Reputation: 1092
Lets say i have a list containing names and Ids. Like this:
l <- structure(list(Kopen = 11, Nodig = 12, `Maat niet Goed? Gratis Retour` = 21,
`Ontdek de Nieuwe Collectie.` = 22, `Bestel NU, Morgen in Huis` = 23,
`Al Meer Dan 1 Miljoen Tevreden Klanten. Ontdek De Mooiste Collectie Van Ons Land` = 31,
`Meer Dan %N% %ad_group%, Altijd Op Voorraad! Scherpe Prijzen.Bestel Nu.` = 32,
`Bestel nu! Alle maten %ad_group% van %merk%, direct uit voorraad leverbaar` = 33), .Names = c("Kopen",
"Nodig", "Maat niet Goed? Gratis Retour", "Ontdek de Nieuwe Collectie.",
"Bestel NU, Morgen in Huis", "Al Meer Dan 1 Miljoen Tevreden Klanten. Ontdek De Mooiste Collectie Van Ons Land",
"Meer Dan %N% %ad_group%, Altijd Op Voorraad! Scherpe Prijzen.Bestel Nu.",
"Bestel nu! Alle maten %ad_group% van %merk%, direct uit voorraad leverbaar"
))
$Kopen
[1] 11
$Nodig
[1] 12
$`Maat niet Goed? Gratis Retour`
[1] 21
$`Ontdek de Nieuwe Collectie.`
[1] 22
$`Bestel NU, Morgen in Huis`
[1] 23
$`Al Meer Dan 1 Miljoen Tevreden Klanten. Ontdek De Mooiste Collectie Van Ons Land`
[1] 31
$`Meer Dan %N% %ad_group%, Altijd Op Voorraad! Scherpe Prijzen.Bestel Nu.`
[1] 32
$`Bestel nu! Alle maten %ad_group% van %merk%, direct uit voorraad leverbaar`
[1] 33
Now i want to use this list to identify elements in my dataframe which looks like this:
df <- structure(list(Headline1 = structure(c(1L, 2L, 1L, 2L, 1L, 2L,
1L, 2L), .Label = c("brand1-Trainingspak Kopen", "brand1-Trainingspak Nodig?"
), class = "factor"), Headline2 = structure(c(2L, 2L, 1L, 1L,
2L, 2L, 1L, 1L), .Label = c("Maat niet Goed? Gratis Retour",
"Ontdek de Nieuwe Collectie."), class = "factor"), Description = structure(c(1L,
1L, 1L, 1L, 2L, 2L, 2L, 2L), .Label = c("Al Meer Dan 1 Miljoen Tevreden Klanten. Ontdek De Mooiste Collectie Van Ons Land",
"Bestel nu! Alle Trainingspakken van brand1 ®, direct uit voorraad leverbaar."
), class = "factor")), .Names = c("Headline1", "Headline2", "Description"
), row.names = c(NA, -8L), class = "data.frame")
The ids are the values in the list, e.g. 11,12,21,22,23,31,32,33.
The result i expect is:
Headline1 Headline1_id Headline2 Headline2_id
Kopen 11 Ontdek de Nieuwe Collectie. 22
Nodig 12 Ontdek de Nieuwe Collectie. 22
I tried to use pmatch, %in%, match, grep and grepl
in combinations with sapply, lapply and vapply
. But unfortunately i couldn't get the patterns to match properly. Is there any quick solution available for this?
So to wrap up once more:
names(l) should be matched against Headline1
, Headline2
and Description
separately. Then create a new column and add the id if an element is identified.
Upvotes: 2
Views: 348
Reputation: 140
Don't know if you are asking for this (or maybe it gives you some ideas):
cbind(df, apply(df, 1:2, getElement, object = l))[c(1,4,2,5,3,6)]
Upvotes: 2