lll
lll

Reputation: 1109

extract numbers/ characters out from the same string of different format

I am new to R, and I have a dataset like the following:

  Artist                                                                              Medium.Size
  1     HIROSHI SUGIMOTO (B. 1948)                     gelatin silver print mounted on paper \n 20 x 24 in. (50.8 x 61 cm.)
  2     HIROSHI SUGIMOTO (B. 1948)                     gelatin silver print mounted on paper \n 20 x 24 in. (50.8 x 61 cm.)
  3     HIROSHI SUGIMOTO (B. 1948)                                 gelatin silver print \n 20 x 24 inches (50.7 x 63.2 cm.)
  4     HIROSHI SUGIMOTO (B. 1948)                                 gelatin silver print \n 20 x 24 inches (50.7 x 63.2 cm.)
  5     HIROSHI SUGIMOTO (B. 1948)                   gelatin silver print mounted on paper \n 20 x 24 in. (50.8 x 60.9 cm.)
  6     HIROSHI SUGIMOTO (B. 1948)                     gelatin silver print mounted on paper \n 20 x 24 in. (50.8 x 61 cm.)
  7     Richard Phillips (b. 1963)                                       graphite on paper \n 12 x 8? in. (30.4 x 21.5 cm.)
  8        Marlene Dumas (b. 1953)                       ink, acrylic and graphite on paper \n 26 x 19? in. (66 x 50.1 cm.)
  9       Lisa Yuskavage (b. 1962)                            oil and graphite on panel \n 7 5/8 x 9? in. (19.3 x 24.7 cm.)
  10      Lisa Yuskavage (b. 1962)                    watercolor and graphite on paper \n 7 5/8 x 10? in. (19.3 x 26.6 cm.)
  11      Barnaby Furnas (b. 1973)                      urethane and wax medium on canvas \n 40 x 30 in. (101.6 x 76.2 cm.)

I would like to extract information in the the second column and get information about medium - words before the first "\n" and expressions in the brackets.

I have tried to use

 split = strsplit(impression$Medium.Size, ", | \n | \\(")

but it seems that it returns to me a list of varying sizes

 [[3517]]
 [1] "oil on canvas\n 25 ? x 32 in." "65.4 x 81.3 cm.)"             

 [[3518]] 
 [1] "bronze with green and brown patina\n Height: 15 in." "38 cm.); Length:  25 5/8"                            
 [3] "65 cm.); Width: 27 5/8 in."                          "70 cm.)"   

what I hope to get are something like

  medium                size
 graphite on paper     50.8*61cm

Upvotes: 1

Views: 59

Answers (1)

Jaap
Jaap

Reputation: 83215

You could use the splitstackshape-package for that as follows:

library(splitstackshape)
cSplit(impression, "Medium", sep = "\n", direction = "wide", fixed = TRUE)

This will give you a data.table in which the Medium-column is split into two columns.

Upvotes: 5

Related Questions