Tom
Tom

Reputation: 329

R: Extract list element but without index number in output

This seems to be a beginner question but I couldn't find the answer anywhere. I know how to extract an element from a list using listname[[1]] but output will always include the index number like

[1] First element of the list

Same is true for using the name like listname$name or unlist(listname$name). All I want is

First element of the list

I could of course remove the index number using regex but I doubt that this is the way it should be :-)

Upvotes: 2

Views: 1737

Answers (1)

Scott Ritchie
Scott Ritchie

Reputation: 10543

The reason that the [1] appears is because all atomic types in R are vectors (of type character, numeric, etc), i.e. in your case a vector of length one.

If you want to see the output without the [1] the simples way is to cat the object:

> listname <- list("This is the first list element", 
                   "This is the second list element")
> cat(listname[[1]], "\n")
This is the first list element 
> 

Upvotes: 3

Related Questions