Importing one long line of data into R

前端 未结 6 1683
醉酒成梦
醉酒成梦 2021-02-05 18:58

I have a large data file consisting of a single line of text. The format resembles

Cat    14  Dog    15  Horse  16

I\'d eventually like to get

6条回答
  •  無奈伤痛
    2021-02-05 19:43

    This solution takes full advantage of scan()'s what argument, and seems simpler (to me) than any of the others:

    x <- scan(file = textConnection("Cat 14 Dog 15 Horse 16"), 
              what = list(Animal=character(), Number=numeric()))
    
    # Convert x (at this point a list) into a data.frame
    as.data.frame(x)
    #   Animal Number
    # 1    Cat     14
    # 2    Dog     15
    # 3  Horse     16
    

提交回复
热议问题