Numbers in quotes read as numerical variable in R - r

I have a dataset where there are many columns with numbers in quotes which indicates that a variable is a factor. (ex: "8").
read.table automatically converts them in numerical variables even if stringsAsFactor is set as true.
Suppose I cannot convert them manually with as.factor, how can I import this dataset with those variables coded directly as factor?

That's because of the quote option. Set quote="". Example:
t <- '"1" "3"
"2" "4"'
> str(read.table(text=t))
'data.frame': 2 obs. of 2 variables:
$ V1: int 1 2
$ V2: int 3 4
> str(read.table(text=t, quote=""))
'data.frame': 2 obs. of 2 variables:
$ V1: Factor w/ 2 levels "\"1\"","\"2\"": 1 2
$ V2: Factor w/ 2 levels "\"3\"","\"4\"": 1 2

Related

How do I convert factorial variables into presence-absence data in r?

I've got a dataset with an abundance variable where the data is ordinal (0, 1-5, 6-10, 10+) but I need to convert it into presence/ absence data (0 or 1). How do I go about doing this?
Here is the data:
'data.frame': 100 obs. of 3 variables:
$ date : Date, format: "2021-02-11" "2021-02-15" "2021-02-16" "2021-02-15" ...
$ abund : Factor w/ 4 levels "0","1-5","6-10",..: 4 1 3 3 4 1 4 2 1 3 ...
$ postcode: chr "EH12 7ET" "NW1 1HP" "TA21 0AS" "LE7 3SY" ...
your_data$abund <- ifelse(your_data$abund=="0",0,1)
or
your_data$abund <- as.numeric(your_data$abund!="0")
The latter works because as.numeric() converts FALSE to 0 and TRUE to 1.
or use transform(your_data, abund=...) (base R) or your_data %>% mutate(across(abund,~1-as.numeric(.=="0)) or ...

Read in Excel column with numbers and characters to R

I'm trying to read in an excel file to R using read_excel(it's a xlsx file), I have columns that contain letters and numbers, for example things like P765876. These columns also have cells with just numbers i.e 234654, so when it reads in to R it reads as an Unknown (not character or numeric) but this means that it gives any cell which has a letter and number a value of NA, how can I read this in correctly?
My code at the moment is
tenant<-read_excel("C:/Users/MPritchard/Repairs Projects/May 2017/Tenant Info/R data 1.xlsx")
Would also recommend to use the col_types argument, by specifying it as "text" you should avoid getting NAs introduced by coercion. So your code would be like:
tenant<-read_excel("C:/Users/MPritchard/Repairs Projects/May 2017/Tenant Info/R data 1.xlsx", col_types = "text")
Please let me know if this solved your problem.
Regards,
/Michael
Not really an answer but too much for a comment...
1:
> library(xlsx)
> tenant <- read.xlsx("returns.xlsx", sheetIndex = 1)
> str(tenant)
'data.frame': 9 obs. of 3 variables:
$ only_integer: num 1 2 34 5 546931 ...
$ int_char : Factor w/ 9 levels "2545","2a","2d",..: 6 4 9 3 5 1 7 2 8
$ only_char : Factor w/ 6 levels "af","dd","e",..: 2 1 5 6 3 2 4 3 1
2:
> library(readxl)
> tenant2 <- read_excel("returns.xlsx")
> str(tenant2)
Classes ‘tbl_df’, ‘tbl’ and 'data.frame': 9 obs. of 3 variables:
$ only_integer: num 1 2 34 5 546931 ...
$ int_char : chr "d5" "5" "ff2ad2f" "2d" ...
$ only_char : chr "dd" "af" "h" "ha" ...
The column int_char is a mixture of both, starting/ending with numbers or characters

Create Matrix of factors from data frame in R

I want to convert a factor data frame, to a factor matrix
But when I try the below code, the type of the matrix is still string
mydata=data.frame(f1=c("yes","yes","no","no"),f2=c("yes","no","no","yes"))
mydata[1:ncol(mydata)]=lapply(mydata[1:ncol(mydata)],factor)
mymatrix=as.matrix(mydata)
#this line didn't help (the matrix still string)
mymatrix=apply(mymatrix,FUN =factor,MARGIN = 2)
Maybe this will get you what you need?
mymatrix = matrix(mydata, ncol = 2)
str(mymatrix)
gives you
List of 2
$ : Factor w/ 2 levels "no","yes": 2 2 1 1
$ : Factor w/ 2 levels "no","yes": 2 1 1 2
- attr(*, "dim")= int [1:2] 1 2
You would need to explain a bit more what you want to do to get more precise help.

Using "NA" as a legitimate nonmissing value

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.
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

Override [.data.frame to drop unused factor levels by default

The issue of dropping unused factor levels when subsetting has come up before. Common solutions include using character vectors where possible by declaring
options(stringsAsFactors = FALSE)
Sometimes, though, ordered factors are necessary for plotting, in which case we can use convenience functions like droplevels to create a wrapper for subset:
subsetDrop <- function(...){droplevels(subset(...))}
I realize that subsetDrop mostly solves this problem, but there are some situations where subsetting via [ is more convenient (and less typing!).
My question is how much further, for the sake of convenience, can we push this to be the 'default' behavior of R by overriding [ for data frames to automatically drop factor levels. For instance, the Hmisc package contains dropUnusedLevels which overrides [.factor for subsetting a single factor (which is no longer necessary, since the default [.factor appears to have a drop argument for dropping unused levels). I'm looking for a similar solution that would allow me to subset data frames using [ but automatically dropping unused factor levels (and of course preserving order in the case of ordered factors).
I'd be really wary of changing the default behavior; you never know when another function you use depends on the usual default behavior. I'd instead write a similar function to your subsetDrop but for [, like
sel <- function(x, ...) droplevels(x[...])
Then
> d <- data.frame(a=factor(LETTERS[1:5]), b=factor(letters[1:5]))
> str(d[1:2,])
'data.frame': 2 obs. of 2 variables:
$ a: Factor w/ 5 levels "A","B","C","D",..: 1 2
$ b: Factor w/ 5 levels "a","b","c","d",..: 1 2
> str(sel(d,1:2,))
'data.frame': 2 obs. of 2 variables:
$ a: Factor w/ 2 levels "A","B": 1 2
$ b: Factor w/ 2 levels "a","b": 1 2
If you really want to change the default, you could do something like
foo <- `[.data.frame`
`[.data.frame` <- function(...) droplevels(foo(...))
but make sure you know how namespaces work as this will work for anything called from the global namespace but the version in the base namespace is unchanged. Which might be a good thing, but it's something you want to make sure you understand. After this change the output is as you'd like.
> str(d[1:2,])
'data.frame': 2 obs. of 2 variables:
$ a: Factor w/ 2 levels "A","B": 1 2
$ b: Factor w/ 2 levels "a","b": 1 2
you can do that work by overwriting the default value of the drop argument like this:
formals(`[.factor`)$drop <- TRUE
UPDATE
as for data.frame, you can do by:
`[.data.frame` <- function(...)droplevels(base::`[.data.frame`(...))
actually similar as #Aaron's one.
if you want to cancel this behavior, then:
rm(`[.data.frame`)
will do that.
> d <- data.frame(a=letters[1:10], b=LETTERS[1:10])
> str(d[1:5, ])
'data.frame': 5 obs. of 2 variables:
$ a: Factor w/ 10 levels "a","b","c","d",..: 1 2 3 4 5
$ b: Factor w/ 10 levels "A","B","C","D",..: 1 2 3 4 5
> `[.data.frame` <- function(...)droplevels(base::`[.data.frame`(...))
> str(d[1:5, ])
'data.frame': 5 obs. of 2 variables:
$ a: Factor w/ 5 levels "a","b","c","d",..: 1 2 3 4 5
$ b: Factor w/ 5 levels "A","B","C","D",..: 1 2 3 4 5
> rm(`[.data.frame`)
> str(d[1:5, ])
'data.frame': 5 obs. of 2 variables:
$ a: Factor w/ 10 levels "a","b","c","d",..: 1 2 3 4 5
$ b: Factor w/ 10 levels "A","B","C","D",..: 1 2 3 4 5
I think that changing the default is very dangerous, see my response here.
Most cases where people are concerned with dropping factor levels you either really don't need to (sumarizing something that you forced to have 1 value is silly) or there is a better way to accomplish what you are trying. The possible side effects from auto dropping is potentially worse than the couple of keystrokes saved. Also if you are doing reproducable research then you should not be depending on or even allowing the computer to change data without your specific say so.

Resources