Rilcon42 Rilcon42 - 1 month ago 15
R Question

Parsing text into dataframe columns

What is the right way to parse this into a two column dataframe? If I split based on spaces it splits too many times, but if I don't split at all everything stays in one column. Would I have to download it with one line, then split on the first space via regex, or is there a better method?

item.ids<-read.csv("http://eve-files.com/chribba/typeid.txt",sep =' ',header=F)
View(item.ids)

Answer

Read it in as fixed-width. Using readr (translate to base if you like counting characters):

library(readr)

df <- read_fwf('http://eve-files.com/chribba/typeid.txt', 
               fwf_empty('http://eve-files.com/chribba/typeid.txt', 
                         skip = 2, 
                         col_names = c('typeID', 'typeName')), 
               skip = 2)

df

## # A tibble: 22,385 × 2
##    typeID           typeName
##     <int>              <chr>
## 1       0            #System
## 2       2        Corporation
## 3       3             Region
## 4       4      Constellation
## 5       5       Solar System
## 6       6    Sun G5 (Yellow)
## 7       7    Sun K7 (Orange)
## 8       8 Sun K5 (Red Giant)
## 9       9      Sun B0 (Blue)
## 10     10     Sun F0 (White)
## # ... with 22,375 more rows