Get a list of numbers from range(s) of number

后端 未结 3 1369
青春惊慌失措
青春惊慌失措 2021-01-21 00:31

I have a data frame where one column contains a range (or ranges) of numbers. I would like to turn this into a list of numbers based of the given range.

Example input:

3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-21 01:06

    we use substr to extract parts of string to get beginning and end of numeric list. we use as.numeric to convert strings extracted to numbers. we use the colon to create the list of numbers . it will also work for multiple parts of list

    > input
    [1] "35-40"
    > instart=substr(input,1,2)
    > instart
    [1] "35"
    > inend=substr(input,4,5)
    > inend
    [1] "40"
    > newlist=as.numeric(instart):as.numeric(inend)
    > newlist
    [1] 35 36 37 38 39 40
    

提交回复
热议问题