Hi everybody,
i feel this is very baisc but i need a hand.
I want to use the strsplitfunction on characters in a data.frame but under certain conditions!
If the condition is met --> split character and take the first two stringparts
If the condition is not met --> split the character and take only the first stringparts
Problem: Only one element of my list standard.modellliste is taken.
Please see my code i produced so far:
I understand that the if-argument does only take one element of a list.
I tried with ifelse and neting it but it was not working either.
Intended result is the following: modell("320 i", "117 e", "A 4", "Focus" ) (bmw and audi-modells have two stringparts - ford only one)
So guys - how can i do a loop and use a list of certain arguments (conditions) for directing the correct functions on my data.frame?
Thank you!
i feel this is very baisc but i need a hand.
I want to use the strsplitfunction on characters in a data.frame but under certain conditions!
If the condition is met --> split character and take the first two stringparts
If the condition is not met --> split the character and take only the first stringparts
Problem: Only one element of my list standard.modellliste is taken.
Please see my code i produced so far:
Code:
autos<-data.frame(hersteller=c("bmw","bmw", "audi", "ford"), modell= c("320 i", "117 e", "A 4", "Focus V1"),stringsAsFactors=FALSE)
standard.modellliste<-c("audi","bmw")
super.simple.modelle<-NULL
for (i in 1:length(autos[,1])){
for (ii in 1:2){
if(autos[,1][i]==standard.modellliste[[ii]]){
super.simple.modelle[i]<-paste(strsplit(autos[,2]," ")[[i]][1],strsplit(autos[,2]," ")[[i]][2])
}else{
super.simple.modelle[i]<-strsplit(autos[,2]," ")[[i]][1]
}}}
super.simple.modelle
I tried with ifelse and neting it but it was not working either.
Code:
for (i in 1:4){
tester<-ifelse(autos[,1]=='bmw', paste(strsplit(autos[,2][[i]]," ")[1],strsplit(autos[,2]," ")[[i]][2]),
ifelse(autos[,1] == 'audi', paste(strsplit(autos[[i]][,2]," ")[1],strsplit(autos[,2]," ")[[i]][2]),strsplit(autos[,2]," ")[[i]][2])
)}
tester
So guys - how can i do a loop and use a list of certain arguments (conditions) for directing the correct functions on my data.frame?
Thank you!