Cant change name of colnames in text file using R

my text file :

jack,28,25.2
smith,23,10.5
lena,49,11
leo,29,21.9
sara,38,44
ibra,23,11.5
Ayan,29,45
sample <- read.table("sample.txt", sep = "|",  header=F)
colnames(sample) <- c("name","age","income")

i try to change name of col but i have error:

'names' attribute [3] must be the same length as the vector [1]

>Solution :

Welcome to Stackoverflow, Abdullah Alotaibi!

As mentioned in the comments you should use read.csv("sample.txt", header=FALSE) or read.csv("sample.txt",sep=",", header=FALSE) .

Because of the use of , as seperator in your textfile.

Leave a Reply