Back

Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
in R Programming by (7.3k points)

I have a problem using data.table: How do I convert column classes? Here is a simple example: With data.frame I don't have a problem converting it, with data.table I just don't know how:

df <- data.frame(ID=c(rep("A", 5), rep("B",5)), Quarter=c(1:5, 1:5), value=rnorm(10))

df <- data.frame(lapply(df, as.character), stringsAsFactors=FALSE)

#One way

df[, "value"] <- as.numeric(df[, "value"])

library(data.table)

dt <- data.table(ID=c(rep("A", 5), rep("B",5)), Quarter=c(1:5, 1:5), value=rnorm(10))

dt <- data.table(lapply(dt, as.character), stringsAsFactors=FALSE) 

#Error in rep("", ncol(xi)) : invalid 'times' argument

#Produces error, does data.table not have the option stringsAsFactors?

dt[, "ID", with=FALSE] <- as.character(dt[, "ID", with=FALSE]) 

#Produces error: Error in `[<-.data.table`(`*tmp*`, , "ID", with = FALSE, value = "c(1, 1, 1, 1, 1, 2, 2, 2, 2, 2)") : 

#unused argument(s) (with = FALSE)

Do I miss something obvious here?

Update due to Matthew's post: I used an older version before, but even after updating to 1.6.6 (the version I use now) I still get an error.

Update 2: Let's say I want to convert every column of class "factor" to a "character" column, but don't know in advance which column is of which class. With data.frame, I can do the following:

classes <- as.character(sapply(df, class))

colClasses <- which(classes=="factor")

df[, colClasses] <- sapply(df[, colClasses], as.character)

Can I do something similar with data.table?

1 Answer

0 votes
by

To convert column classes in data.table, you can do the following:

library(data.table)

dt <- data.table(ID=c(rep("A", 5), rep("B",5)),

                 Quarter=c(1:5, 1:5),

                 value=rnorm(10))

str(dt)

Classes ‘data.table’ and 'data.frame': 10 obs. of  3 variables:

 $ ID     : chr  "A" "A" "A" "A" ...

 $ Quarter: int  1 2 3 4 5 1 2 3 4 5 #integer column

 $ value  : num  1.715 0.461 -1.265 -0.687 -0.446 ...

 - attr(*, ".internal.selfref")=<externalptr> 

To convert one column:

dt1 <- dt[, Quarter:=as.character(Quarter)]

str(dt1)

Classes ‘data.table’ and 'data.frame': 10 obs. of  3 variables:

 $ ID     : chr  "A" "A" "A" "A" ...

 $ Quarter: chr  "1" "2" "3" "4" …    #converted to character

 $ value  : num  1.715 0.461 -1.265 -0.687 -0.446 ...

 - attr(*, ".internal.selfref")=<externalptr> 

To convert multiple columns:

You can use the lapply function with .SD which stands for "Subset of Data.table".i.e.,

 dtnew <- dt[, lapply(.SD, as.character), by=ID]

 str(dtnew)

Classes ‘data.table’ and 'data.frame': 10 obs. of  3 variables:

 $ ID     : chr  "A" "A" "A" "A" ...

 $ Quarter: chr  "1" "2" "3" "4" ...

 $ value  : chr  "1.78691313680308" "0.497850478229239" "-1.96661715662964" "0.701355901563686" ...

 - attr(*, ".internal.selfref")=<externalptr> 

Related questions

Welcome to Intellipaat Community. Get your technical queries answered by top developers!

28.4k questions

29.7k answers

500 comments

94.1k users

Browse Categories

...