-1

This is probably a basic question, but why does R think my vector, which has a bunch of words in it, are numbers when I try to use these vectors as column names?

I imported a data set and it turns out the first row of data are the column headers that I want. The column headers that came with the data set are wrong ones. So I want to replace the column names. I figured this should be easy.

So what I did was I extracted the first row of data into a new object:

names <- data[1,]

Then I deleted the first row of data:

data <- data[-1,]

Then I tried to rename the column headers with the "names" object:

colnames(data) <- names

However, when I do this, instead of changing my column names to the words within the names object, it turns it into a bunch of numbers. I have no idea where these numbers come from.

Thanks

smci
  • 32,567
  • 20
  • 113
  • 146
user6472523
  • 211
  • 3
  • 8
  • *"I imported a data set and it turns out the first row of data are the column headers that I want."*. Show us the first two rows of your data, with `dput()` and `head()`. Also show us the `read.csv()/read.table()` import command you used, did you set `header=TRUE`?. – smci Mar 31 '18 at 02:43
  • 1
    I think the problem is that your columns are factors. Try with `data <- strings("finelname.csv", head = TRUE, stringsAsFactors = FALSE)` – kangaroo_cliff Mar 31 '18 at 02:52
  • 1
    @Suren: there is no function `base::strings()`, which package do you mean? In any case `read.csv/read.table` is in `utils`, and they already said that solved their issue. – smci Mar 31 '18 at 02:54
  • Oops, I didn't mean `strings`. Not sure how that typo came. I meant, `data <- read.csv("finelname.csv", head = TRUE, stringsAsFactors = FALSE)` – kangaroo_cliff Mar 31 '18 at 04:30

1 Answers1

0

You need to actually show us the data, and the read.csv()/read.table() command you used to import.

If R thinks your numeric column is string, it sounds like that's because it wrongly includes the column name, i.e. you omitted header=TRUE in your read.csv()/read.table() import.

But show us your actual data and commands used.

smci
  • 32,567
  • 20
  • 113
  • 146