1

I want to create 4 varaibles out of 1 one varaibe.

Variable Var1 var2 var3 var4 Upholstery KCDF  Venetian Beige Leatherette N/C Upholstery KCDF Venetian Beige Leatherette N/C  Accessories Z06    Black Kidney Grilles 173.00  Accessories Z06  Black Kidney Grilles 173

or splitting " Accessories Z06    Black Kidney Grilles 173.00" this into  Accessories Z06  Black Kidney Grilles 173

1
  • You mentioned creating 4 variables out of 1 variable. It is not clear where the splitting should occur in the example "Accessories Z06 ......173.00". If you split by space, then it should be 6 variables. Commented Jul 19, 2014 at 15:30

4 Answers 4

1

Since you did not provide your data in a form we can reproduce, I made a mock set:

> furn <- c("Upholstery KCDF", "Venetian Beige Leatherette N/C", "Upholstery KCDF", "Venetian Beige Leatherette N/C",  "Accessories Z06", "Black Kidney Grilles 173.00", "Accessories Z06", "Black Kidney Grilles 173")

Then, with the stringi package you can separate words at the spaces and unlist the result as a vector:

> require(string1)

> unlist(stri_split_regex(str = furn, pattern = " "))
 [1] "Upholstery"  "KCDF"        "Venetian"    "Beige"       "Leatherette" "N/C"         "Upholstery"  "KCDF"       
 [9] "Venetian"    "Beige"       "Leatherette" "N/C"         "Accessories" "Z06"         "Black"       "Kidney"     
[17] "Grilles"     "173.00"      "Accessories" "Z06"         "Black"       "Kidney"      "Grilles"     "173"   
Sign up to request clarification or add additional context in comments.

Comments

0

You are looking for strsplit

split.var <- strsplit(my.var, " ")

However, this will split at all spaces, so you may have to recode your original data to use a different separator.

Comments

0

This example will help you.

library(base)
a<-"welcome to stack overflow"
b<-strsplit(a," ")
b<-unlist(b)
b1<-as.matrix(a)
a1<-b[1]
a2<-b[2]
a3<-b[3]
a4<-b[4]

Comments

0

If I understand your question correctly: the single string should be formatted in a table as:

       Variable      Var1      var2        var3   var4 
Upholstery KCDF  Venetian     Beige Leatherette    N/C 
Upholstery KCDF  Venetian     Beige Leatherette    N/C  
Accessories Z06     Black    Kidney Grilles     173.00  
Accessories Z06     Black    Kidney Grilles        173

Now, if the data is was in a deliminated file the function read.table should do what you need.

However, if the data is a single string such as:

input <- "Variable Var1 var2 var3 var4 Upholstery KCDF  Venetian Beige Leatherette N/C Upholstery KCDF Venetian Beige Leatherette N/C  Accessories Z06    Black Kidney Grilles 173.00  Accessories Z06  Black Kidney Grilles 173"

Then we have a different problem and a different solution will be needed.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.