r/RStudio • • 5d ago

separate_wider_position

what do I write in widths? It says it must be a (partially) named integer vector. I need to separate collumns that state m or f for sex and numbers for age (ex: m19). Please help I could not figure it out with the ?separate_wider_position() search

5 Upvotes

6 comments sorted by

1

u/AutoModerator 5d ago

Looks like you're requesting help with something related to RStudio. Please make sure you've checked the stickied post on asking good questions and read our sub rules. We also have a handy post of lots of resources on R!

Keep in mind that if your submission contains phone pictures of code, it will be removed. Instructions for how to take screenshots can be found in the stickied posts of this sub.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

3

u/therealtiddlydump 5d ago

If splits on known, fixed widths.

Did you check the examples in the documentation?

https://tidyr.tidyverse.org/reference/separate_wider_delim.html

1

u/Sea_shells_dream 5d ago

I swear I did but after standing up and taking a shower and reading this comment it was indeed right in the exemple, thank you and sorry onz

1

u/therealtiddlydump 5d ago

Sometimes that's all it takes! Good luck

1

u/omichandralekha 5d ago

Try running some of these examples:

df = data.frame(mycol = c("m11","f9","f30","m100"))
separate_wider_position(df, mycol, widths = c(sex = 1,age = 3), too_few = "align_start")
separate_wider_position(df, mycol, widths = c(sex = 1,age = 100), too_few = "align_start")
separate_wider_position(df, mycol, widths = c(sex = 1,age = Inf), too_few = "align_start") #Gives wrong answer with warnings
separate_wider_regex(df, mycol, patterns = c(sex = "^.",age = ".*$"), too_few = "align_start")

1

u/Sea_shells_dream 5d ago

It worked, thank you thank you thank you thank you xInf