1

I have a Character List in the format of deck/number/side, and I'd like to create separate variables for deck and side.

the spaceTrainTib$Cabin Variable looks like:

[1] "B/0/P" "F/0/S" "A/0/S" "A/0/S" "F/1/S" "F/0/P" "F/2/S" "G/0/S"
[9] "F/3/S" "B/1/P" "B/1/P" "B/1/P" "F/1/P" "G/1/S" "F/2/P" NA
[17] "F/3/P" "F/4/P" "F/5/P" "G/0/P" "F/6/P" "E/0/S" "E/0/S" "E/0/S"
[25] "E/0/S" "E/0/S" "E/0/S" "D/0/P" "C/2/S" "F/6/S" "C/0/P" "F/8/P"
[33] "G/4/S" "F/9/P" "F/9/P" "F/9/P" "D/1/S" "D/1/P" "F/8/S" "F/10/S" [41] "G/1/P" "G/2/P" "B/3/P" "G/3/P" "G/3/P" "G/3/P" "F/10/P" "F/10/P"

deck <- map_chr(str_split(spaceTrainTib$Cabin, "/"), 1)
deck

returning

[1] "B" "F" "A" "A" "F" "F" "F" "G" "F" "B" "B" "B" "F" "G" "F" NA  "F" "F" "F" "G" "F"
  [22] "E" "E" "E" "E" "E" "E" "D" "C" "F" "C" "F" "G" "F" "F" "F" "D" "D" "F" "F" "G" "G"
  [43] "B" "G" "G" "G" "F" "F" "E" "E" "G" "F" "A" "A" "A" "G" "F" "F" "F" "E" "G" "G" "G"

is doing it's job. But it will not work if I use

side <- map_chr(str_split(spaceTrainTib$Cabin, "/"), 3)
side

The error ist: "Error in `stop_bad_type()' ! Result 16 must be a single string, not NULL of length 0

to extract the third letter. Can anyone help?

  • It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. What exactly does "not work" mean is this case? Do you get an error? Or an unexpected results? What exactly happens? – MrFlick Nov 14 '22 at 15:23
  • I think that what you're looking for is `separate`, instead of `map_chr`. For example, `spaceTrainTib %>% separate(Cabin, into = c("this", "that", "andAnother"), sep = "/")` You can remove the value if you don't want to include it with `select(-that)` if you want to keep the original variable, then add `remove = F`. – Kat Nov 14 '22 at 22:20

1 Answers1

0

I'm guessing your looking for something like this:

# using some of your data
some_string <- c("B/0/P", "F/0/S", "A/0/S", "A/0/S", "F/1/S", "F/0/P", "F/2/S", "G/0/S",
"F/3/S", "B/1/P", "B/1/P", "B/1/P", "F/1/P", "G/1/S", "F/2/P", NA)
# as experiment we try
unlist(strsplit(some_string, ' '))
 [1] "B/0/P" "F/0/S" "A/0/S" "A/0/S" "F/1/S" "F/0/P" "F/2/S" "G/0/S" "F/3/S"
[10] "B/1/P" "B/1/P" "B/1/P" "F/1/P" "G/1/S" "F/2/P" NA
#then remove `/` from each triplet
new_string <- gsub('/', '', unlist(strsplit(some_string, ' ')))
 [1] "B0P" "F0S" "A0S" "A0S" "F1S" "F0P" "F2S" "G0S" "F3S" "B1P" "B1P" "B1P"
[13] "F1P" "G1S" "F2P" NA 

deck <- substr(next_string, 1,1)
deck
 [1] "B" "F" "A" "A" "F" "F" "F" "G" "F" "B" "B" "B" "F" "G" "F" NA 
side <- substr(next_string, 3, 3)
side
 [1] "P" "S" "S" "S" "S" "P" "S" "S" "S" "P" "P" "P" "P" "S" "P" NA

an approach.

Chris
  • 1,647
  • 1
  • 18
  • 25
  • Actually I'm trying to store the third letter of as a seperate Variable. Just like I did with the first letter as in "deck" – Niclas Kammler Nov 14 '22 at 16:05