-2

I have a data frame with character and numeric columns, when plotting a PCA later on, I need to plot character columns so I wanted to convert column "station" to character.

The data frame:

coldata <- data.frame(Location = c(West,West,East,East),
                          Station = c(1,2,3,4),
                          A = c("0.3","0.2","0.8","1"))

And the type of columns:

sapply(coldata, mode) location station A
    "character" "numeric" "numeric"

I would like to have this:

sapply(coldata, mode) location station A
"character" "character" "numeric"

Can a column be labelled as "character" if containing numbers? Or would I have to convert the numbers into words: ie: 1, 2, 3,4 to one, two, three, four?

I have seen many posts converting character into numeric, but not numeric to character.

Ecg
  • 908
  • 1
  • 10
  • 28
  • `coldata$Station <- as.character(coldata$Station)` – IceCreamToucan Aug 29 '19 at 13:35
  • Just do `coldata$Station <- as.character(coldata$Station)` as per usual – Sotos Aug 29 '19 at 13:35
  • `"4"` is different than `4` here, no need to spell out numbers. – cory Aug 29 '19 at 13:38
  • Yes ``"1"`` is a character while ``1`` is a numeric (integer). You can simply do like Sotos said if you simply want to modify one column. This question is a duplicate tho. – Gainz Aug 29 '19 at 14:12
  • note that `A` may not be numeric in the sense you can believe it is: as you put on quotes, it's been imported as factor so you won't have 0.3, 0.2, etc. but 1, 2, 3, 4 (factor levels) (or is it just a typo in your example, as the missing quotes for `Location` ?) – Cath Aug 29 '19 at 14:41
  • @Cath it was a typo on the example, station and A are both numeric therefore without "". Thanks – Ecg Aug 30 '19 at 07:32

1 Answers1

6

I think you can do that and that should work :

coldata$Station<-as.character(coldata$Station)
Jérémy
  • 340
  • 1
  • 3
  • 13