-2

I have 1000+ rows of string which I extracted from a column of an Excel worksheet. Here's how the data looks like (3 rows):

Chicken(31%);Duck(16%);Wild duck(14%);Turkey(10%);Pigeon(4%);Goose(4%);Wild bird(4%);Tree sparrow(2%)

Tree sparrow(2%)

Chicken(1%)

I need to put the data into a table (for this example: 8 columns x 3 rows). Can anyone help?

x <- c("Chicken(31%);Duck(16%);Wild duck(14%);Turkey(10%);Pigeon(4%);Goose(4%);Wild bird(4%);Tree sparrow(2%)", 
"Tree sparrow(2%)", "Chicken(1%)")
1
  • What have you tried? Is the semicolon the separator for columns? What values do you want to fill with if a row has less than 8 entries?
    – blep
    Sep 5, 2015 at 18:55

2 Answers 2

2

There is most likely more concise way but you can try something like this:

library(stringi)
library(data.table)

# Drop empty lines if any
txt <- Filter(function(x) !stri_isempty(stri_trim(x)),  x)
# Extract matches
matches <- stri_match_all_regex(txt, "([\\w\\s]+)\\(([1-9]+)%\\);?")

matches[[1]]

##      [,1]               [,2]           [,3]
## [1,] "Chicken(31%);"    "Chicken"      "31"
## [2,] "Duck(16%);"       "Duck"         "16"
## [3,] "Wild duck(14%);"  "Wild duck"    "14"
## [4,] "Pigeon(4%);"      "Pigeon"       "4" 
## [5,] "Goose(4%);"       "Goose"        "4" 
## [6,] "Wild bird(4%);"   "Wild bird"    "4" 
## [7,] "Tree sparrow(2%)" "Tree sparrow" "2" 

# Rearrange
rows <- lapply(
   matches,
   function(x) setNames(as.list(as.numeric(x[, 3])), x[, 2]))

rbindlist(rows, fill=TRUE)

##    Chicken Duck Wild duck Pigeon Goose Wild bird Tree sparrow
## 1:      31   16        14      4     4         4            2
## 2:      NA   NA        NA     NA    NA        NA            2
## 3:       1   NA        NA     NA    NA        NA           NA

Regex explanation

([\\w\\s]+) #  At least one word character or whitespace *, 1st group
\\( # Left parenthesis
([1-9]+) # At least one digit. You can replace + with {1,2}, 2nd group
% # Percent sign
\\) # Right parenthesis
;? # Optional semicolon 

* Could be \\w[\\w\\s]+

7
  • Thanks a lot Tyler Rinker and zero323 for your guidance. Zero323's code is exactly what I want to do. Thank you both!
    – Rbeginner
    Sep 6, 2015 at 4:45
  • Is it possible to explain the regex ([\\w\\s]+)\(([1-9]+)%\);? in details? I adapt your code but getting NA in all lists of my output. Thanks a lot!
    – Rbeginner
    Sep 7, 2015 at 5:41
  • The data is slightly different now (e.g. Chicken (15.25%); Duck (10,24%). There's a space in between the name and %, and% with 2 decimal point. I modified the regex to: ([\\w\\s]+)(\\s)\(([1-9]+)%\);? but still getting NA.
    – Rbeginner
    Sep 7, 2015 at 7:43
  • I modified the regex to "([\\w-\\w\\s]+) \(([1-9]+\\.[0-9][0-9]?)%\);?" and it works now. However, I lost the first word of the string. in this case "Chicken (15.25%)". Any idea :-(
    – Rbeginner
    Sep 7, 2015 at 8:42
  • Something like this: "(\\w[\\w\\s]+)\\(([1-9]+\\.[0-9][0-9]?)%\\);?"?
    – zero323
    Sep 7, 2015 at 16:42
1

Here's on possible solution:

library(qdapTools)
mtabulate(strsplit(gsub("\\(\\d+%\\)", "", x), ";"))



##   Chicken Duck Goose Pigeon Tree sparrow Turkey Wild bird Wild duck
## 1       1    1     1      1            1      1         1         1
## 2       0    0     0      0            1      0         0         0
## 3       1    0     0      0            0      0         0         0

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.