-3

First Query:

File Name is Test which is a data frame and vector is only one containing names like Stacy-A, Mark-B, Giselle-C. I want to split a name lets say Stacy-A and I have more than 50000+ rows. So I want one vector containing Stacy and one vector containing A i.e. characters after split. I have run a loop for this. Since loop returns lists and I have used this.

for(i in 1:nrow(TEST)){TEST[i,"Name"]<-strsplit(TEST[i,"Name"],"-")[[1]][1] 
  TEST[i,"Character"]<-strsplit(TEST[i,"Name"],"-")[[1]][2]}

It takes a lot of time. Can someone let me know how to use sapply or any apply function but I want in data frame and not lists or matrix.

Second Query:

Test is my database which I want values and I have also one file named as User from which I want to pull values.

I want to put a lookup like excel to pick values from another file. I have two conditions in my loop. I find matching values then only I pick it and if there are duplicates I pick only one. I used this loop. It takes 3 hours. I have dataframe and for 300000+ rows in my user file from where I want values.

for (i in 1:nrow(Test)){if(Test[i,"Item_Cd"] %in% User_item_no)
 {item_cd_found<-Test[i,"Item_Cd"]Test[i,"Order.Status"]<-as.character(User[which(User$Item.No.==item_cd_found),"Name"])[1]}
Roman Luštrik
  • 69,533
  • 24
  • 154
  • 197
iamashish
  • 57
  • 7
  • 2
    voting to close, it lacks a [mcve], see [this thread](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) to improve your question. – Tensibai Jan 19 '16 at 10:07

1 Answers1

1

This may be a shot into the dark without a reproducible example, but you may be after something like this. First I split each element by - and take out first and second element and assign it to a respective variable.

xy <- c("Svarog-A", "Knez-B", "Petovia-C", "Svarun-D", "Hotimir-D")

splitxy <- strsplit(xy, "-")

xy.names <- sapply(splitxy, "[", 1)
xy.letters <- sapply(splitxy, "[", 2)

> xy.names
[1] "Svarog"  "Knez"    "Petovia" "Svarun"  "Hotimir"
> xy.letters
[1] "A" "B" "C" "D" "D"
Roman Luštrik
  • 69,533
  • 24
  • 154
  • 197