0

I want to read below data example in r.

The first column is country name, but when i read in r, it give some error of sep?

My code:

df <- read.table('df.txt', header=T)

My data look like

column1 column 2
spain      20
united kingdom 37
germany 97
republic of china 12

Any suggestion please?

GhostCat
  • 137,827
  • 25
  • 176
  • 248
bha
  • 77
  • 2
  • 7
  • Other users marked your question for low quality and need for improvement. I re-worded/formatted your input to make it easier to read/understand. Please review my changes to ensure they reflect your intentions. But I think your question is still not answerable. **You** should [edit] your question now, to add missing details (see [mcve] ). Feel free to drop me a comment in case you have further questions or feedback for me. – GhostCat Oct 31 '18 at 19:33

1 Answers1

0

The problem is there are different number of columns for each row (using the delimiter sep=" "). Try

df = readLines("df.txt")

This will give you a vector of strings, where each element is a row in df.txt. You can parse the lines further with

countries = strsplit(df, " ")
countries = sapply(countries, function(x) paste(x[-length(x)], collapse = " "))

to get the countries.

strsplit returns a list, and sapply(...) removes the last element of item in the list, then pastes them all back together.

Your question is probably a duplicate of this question.

mickey
  • 2,168
  • 2
  • 11
  • 20