开发者

Converting numeric type vector into a vector of strings

开发者 https://www.devze.com 2023-04-08 04:36 出处:网络
I\'m trying to convert this: > j[1:5] NA06985 NA06991 NA06993 NA06994 NA07000 Into this: c(\"NA06985\",\"NA06991\",\"NA06993\", \"NA06994\", \"NA07000\")

I'm trying to convert this:

> j[1:5] 
NA06985 NA06991 NA06993 NA06994 NA07000

Into this:

c("NA06985","NA06991","NA06993", "NA06994", "NA07000")

I've tried using as.character but it gives me:

> as.charac开发者_Go百科ter(j[1:5])
[1] "10" "10" "10" "10" "10"

Help please! -Josh

EDIT: Okay so I think I figured it out. After doing class(j) I found that it was of type data.frame. So I converted to as.matrix and it worked..hooray!


paste(j[1:5])

This works for strings, factors, numerics, pretty much anything that can be displayed.


Okay so I think I figured it out. After doing class(j) I found that it was of type data.frame. So I converted to as.matrix and it worked..hooray!


Assumming that j is a factor

> j <- factor(c("NA06985","NA06991","NA06993", "NA06994", "NA07000", "extra level"))
> j
[1] NA06985     NA06991     NA06993     NA06994     NA07000     extra level
Levels: extra level NA06985 NA06991 NA06993 NA06994 NA07000
> levels(j)[j[1:5]]
[1] "NA06985" "NA06991" "NA06993" "NA06994" "NA07000"
0

精彩评论

暂无评论...
验证码 换一张
取 消