Importing one long line of data into R

前端 未结 6 1685
醉酒成梦
醉酒成梦 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:50

    One way:

    # read the line
    r <- read.csv("exa.Rda",sep=" ", head=F)
    # every odd number index is an animal
    animals <- r[,(1:ncol(r)-1)%%2==0]
    # every even number index is a number
    numbers <- r[,(1:ncol(r))%%2==0]
    # flipping the animal row into a column
    animals <- t(animals)
    # flipping the number row into a column
    numbers <- t(numbers)
    # putting the data together
    mydata <- data.frame(animals, numbers)
    

提交回复
热议问题