Pergunta

I'm working with a data set that includes first names entered in all capital letters. I need to work with the names as character variables, not as factors.

One person in the data set has the first name "NA". Can I get R to accept "NA" as a legitimate character value? My work-around solution was to rename that person NAA, but I am interested to see if there is a better way.

Foi útil?

Solução

As a demonstration of my comment, consider the following sample CSV file:

x <- tempfile()
cat("v1,v2", "NA,1", "AB,3", sep = "\n", file = x)

cat(readLines(x), sep = "\n")
# v1,v2
# NA,1
# AB,3

Here's the str of a basic read.csv. Note the NA is seen as NA

str(read.csv(x))
# 'data.frame':  2 obs. of  2 variables:
#  $ v1: Factor w/ 1 level "AB": NA 1
#  $ v2: int  1 3

Now, specify a different character as your na.strings argument:

str(read.csv(x, na.strings = ""))
# 'data.frame':  2 obs. of  2 variables:
#  $ v1: Factor w/ 2 levels "AB","NA": 2 1
#  $ v2: int  1 3
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top