12

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.character(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!

2
  • 1
    The answer to this will probably be fairly simple if you share the output of str(j). Commented Sep 26, 2011 at 4:27
  • 2
    Can you post exactly what you see when you type j[1:5] ? And what is class(j)? Commented Sep 26, 2011 at 4:28

3 Answers 3

11

paste(j[1:5])

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

Sign up to request clarification or add additional context in comments.

Comments

1

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!

Comments

0

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"

1 Comment

This is fine, but as.character(j[1:5]) would work as well for this case. It seems there is something different about j in the OP's question, or they are simply not reporting the issue accurately.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.