Home > Software design >  How to split a string into triplets
How to split a string into triplets

Time:08-13

I have a string that looks like this (the text is actually much longer):

".296.294.255.268.313.278.311.270.290.305.322.252.276.286.301.305.264.301.251.269.274.311.304.
              230.280.264.327.301.301.265.287.285.306.265.282.319.235.262.278.249.239.284.237.249.289.250.
              282.240.256.287.303.310.314.242.302.289.268.315.264.293.261.298.310.242.253.299.278.272.333.
              272.295.306.276.317.286.250.272.272.274.282.308.262.285.326.321.285.270.270.241.283.305.319.
              246.263.311.299.295.315.263.304.279.286.286.299.282.285.289.298.277.292.296.282.267.245.304.
              322.252.265.313.288.310.281.272.266.243.285.309.295.269.295.308.275.316.267.283.311.300.252."

I need to transform it into X lists, each containing 3 numbers. Example:

List1 <- c(296, 294, 255)

List2 <- c(268, 313, 278)

etc.

I tried to split by delimiter with strsplit(split = "\.") but that gives me 1) break lines that I cannot eliminate and 2) I don't seem to be able to convert the values into numbers.

Any suggestions?

Thanks in advance!

CodePudding user response:

Try asplit scan (given string s)

> asplit(matrix(na.omit(scan(text = s, sep = ".")), 3), 2)
Read 145 items
[[1]]
[1] 296 294 255

[[2]]
[1] 268 313 278

[[3]]
[1] 311 270 290

[[4]]
[1] 305 322 252

[[5]]
[1] 276 286 301

[[6]]
[1] 305 264 301

[[7]]
[1] 251 269 274

[[8]]
[1] 311 304 230

[[9]]
[1] 280 264 327

[[10]]
[1] 301 301 265

[[11]]
[1] 287 285 306

[[12]]
[1] 265 282 319

[[13]]
[1] 235 262 278

[[14]]
[1] 249 239 284

[[15]]
[1] 237 249 289

[[16]]
[1] 250 282 240

[[17]]
[1] 256 287 303

[[18]]
[1] 310 314 242

[[19]]
[1] 302 289 268

[[20]]
[1] 315 264 293

[[21]]
[1] 261 298 310

[[22]]
[1] 242 253 299

[[23]]
[1] 278 272 333

[[24]]
[1] 272 295 306

[[25]]
[1] 276 317 286

[[26]]
[1] 250 272 272

[[27]]
[1] 274 282 308

[[28]]
[1] 262 285 326

[[29]]
[1] 321 285 270

[[30]]
[1] 270 241 283

[[31]]
[1] 305 319 246

[[32]]
[1] 263 311 299

[[33]]
[1] 295 315 263

[[34]]
[1] 304 279 286

[[35]]
[1] 286 299 282

[[36]]
[1] 285 289 298

[[37]]
[1] 277 292 296

[[38]]
[1] 282 267 245

[[39]]
[1] 304 322 252

[[40]]
[1] 265 313 288

[[41]]
[1] 310 281 272

[[42]]
[1] 266 243 285

[[43]]
[1] 309 295 269

[[44]]
[1] 295 308 275

[[45]]
[1] 316 267 283

[[46]]
[1] 311 300 252

CodePudding user response:

asplit(m,1) makes a list for matrix rows:

asplit(matrix(as.integer(strsplit(x,"[.\n ] ")[[1]][-1]),,3,T),1)

CodePudding user response:

With a tidyverse approach:

library(tidyverse)

tibble(s = s %>% trimws(whitespace = "\\.")) %>% 
  separate_rows(s, sep ="\\.", convert = T) %>% 
  group_split((row_number()   2) %/% 3, .keep = F) %>% 
  map(~ pull(.x, s))

#> [[1]]
#> [1] 296 294 255
#> 
#> [[2]]
#> [1] 268 313 278
#> 
#> [[3]]
#> [1] 311 270 290
#> 
#> [[4]]
#> [1] 305 322 252
#> 
#> [[5]]
#> [1] 276 286 301
#> 
#> [[6]]
#> [1] 305 264 301
#> 
#> [[7]]
#> [1] 251 269 274
#> 
#> [[8]]
#> [1] 311 304 230
#> 
#> [[9]]
#> [1] 280 264 327
#> 
#> [[10]]
#> [1] 301 301 265
#> 
#> [[11]]
#> [1] 287 285 306
#> 
#> [[12]]
#> [1] 265 282 319
#> 
#> [[13]]
#> [1] 235 262 278
#> 
#> [[14]]
#> [1] 249 239 284
#> 
#> [[15]]
#> [1] 237 249 289
#> 
#> [[16]]
#> [1] 250 282 240
#> 
#> [[17]]
#> [1] 256 287 303
#> 
#> [[18]]
#> [1] 310 314 242
#> 
#> [[19]]
#> [1] 302 289 268
#> 
#> [[20]]
#> [1] 315 264 293
#> 
#> [[21]]
#> [1] 261 298 310
#> 
#> [[22]]
#> [1] 242 253 299
#> 
#> [[23]]
#> [1] 278 272 333
#> 
#> [[24]]
#> [1] 272 295 306
#> 
#> [[25]]
#> [1] 276 317 286
#> 
#> [[26]]
#> [1] 250 272 272
#> 
#> [[27]]
#> [1] 274 282 308
#> 
#> [[28]]
#> [1] 262 285 326
#> 
#> [[29]]
#> [1] 321 285 270
#> 
#> [[30]]
#> [1] 270 241 283
#> 
#> [[31]]
#> [1] 305 319 246
#> 
#> [[32]]
#> [1] 263 311 299
#> 
#> [[33]]
#> [1] 295 315 263
#> 
#> [[34]]
#> [1] 304 279 286
#> 
#> [[35]]
#> [1] 286 299 282
#> 
#> [[36]]
#> [1] 285 289 298
#> 
#> [[37]]
#> [1] 277 292 296
#> 
#> [[38]]
#> [1] 282 267 245
#> 
#> [[39]]
#> [1] 304 322 252
#> 
#> [[40]]
#> [1] 265 313 288
#> 
#> [[41]]
#> [1] 310 281 272
#> 
#> [[42]]
#> [1] 266 243 285
#> 
#> [[43]]
#> [1] 309 295 269
#> 
#> [[44]]
#> [1] 295 308 275
#> 
#> [[45]]
#> [1] 316 267 283
#> 
#> [[46]]
#> [1] 311 300 252

CodePudding user response:

Here is one approach, but perhaps not the most elegant:

  1. get a vector of numbers;
nums = as.numeric(strsplit(gsub("\\n  ","", k), "\\.")[[1]])
nums <- nums[!is.na(nums)]
  1. use split to make X lists
split(nums, rep(1:(length(nums)/3),each=3))

CodePudding user response:

Slight change in strsplit argument

library(stringr)
nb=".296.294.255.268.313.278.311.270.290.305.322."
split_nb=strsplit(x=nb,split = "[.]")[[1]]
split_nb=split_nb[-1]#remove the first which is empty

# Put vector elements 3 by 3 into objects named list1, list2, etc
for (i in 1:trunc(length(split_nb)/3)){
  assign(x=paste0("list",i), value= c(split_nb[i*3-3 1], split_nb[i*3-3 2], split_nb[i*3-3 3]))
}

# Check it worked
list1
list2
list3
  •  Tags:  
  • r
  • Related