Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

type.convert to character using cSplit

Tags:

r

I have the following columns in my data.frame:

                services_01               services_02               services_03               services_04
             Privacy Policy                        NA                        NA                        NA
  Global Expense Management            Privacy Policy                        NA                        NA
 Exception & Cost Admin      Global Cost Estimate Global Expense Management            Privacy Policy

I created them by using df <- cSplit(df, 'services', ',').

Note that services was a comma-separated list, and after running the cSplit function, I get several new columns.

The problem is, it converts them to factors. I would like them to be characters.

I see the method takes a parameter type.convert but I don't know how to use it.

The documentation says this:

type.convert Logical. Should type.convert be used to convert the result of each column? This would add a little to the execution time.

How can I convert all the factors above to simple characters?

like image 581
user1477388 Avatar asked May 25 '26 13:05

user1477388


2 Answers

You can convert the 'factor' columns to 'character' using the data.table methods as the output of cSplit is a data.table

   df1 <- df[, lapply(.SD, as.character)]
like image 79
akrun Avatar answered May 27 '26 03:05

akrun


Set type.convert = F to keep columns as strings.

like image 32
Suhas Avatar answered May 27 '26 02:05

Suhas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!