

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to separate two values in single column in data.table object in R?
To separate two values in single column in data.table object in R, we can follow the below steps −
First of all, create a data.table object.
Then, use separate function from tidyr package to separate the values in single column.
Example
Create the data.table object
Let’s create a data.table object as shown below −
library(data.table) DT<- data.table(x=sample(c("A,B","B,C","C,D","D,E","E,F","F,G","G,H"),25,replace=TRUE)) DT
Output
On executing, the above script generates the below output(this output will vary on your system due to randomization) −
x 1: C,D 2: C,D 3: C,D 4: A,B 5: F,G 6: A,B 7: C,D 8: G,H 9: B,C 10: C,D 11: G,H 12: G,H 13: G,H 14: C,D 15: F,G 16: G,H 17: C,D 18: C,D 19: F,G 20: G,H 21: E,F 22: A,B 23: G,H 24: D,E 25: F,G x
Separate values in column
Using separate function from tidyr package to separate the values in column x of data.table object DT −
library(data.table) DT<- data.table(x=sample(c("A,B","B,C","C,D","D,E","E,F","F,G","G,H"),25,replace=TRUE)) library(tidyr) DT %>% separate(x,c("Group1","Group2"),sep=",")
Output
Group1 Group2 1: C D 2: C D 3: C D 4: A B 5: F G 6: A B 7: C D 8: G H 9: B C 10: C D 11: G H 12: G H 13: G H 14: C D 15: F G 16: G H 17: C D 18: C D 19: F G 20: G H 21: E F 22: A B 23: G H 24: D E 25: F G Group1 Group2
- Related Questions & Answers
- How to separate two values in single column in R data frame?
- How to separate first text value and the remaining text in R data frame column values?
- Concatenate the column values with separate text in MySQL and display in a single column
- How to repeat column values in R data frame by values in another column?
- How to rename a single column in an R data frame?
- How to separate last name and first names in single column into two new columns in MySQL?
- How to subtract column values from column means in R data frame?
- How to separate a specific text and the remaining text in data.table object column in R?
- How to convert a data frame into two column data frame with values and column name as variable in R?
- Set values in categorical column to numeric values in R data frame.
- How to add single quotes to strings in an R data frame column?
- How to convert data frame into two column data frame with values excluding NAs and column name as variable in R?
- How to select positive values in an R data frame column?
- How to randomly replace values in an R data frame column?
- How to create a boxplot of single column in R data frame with column name?
Advertisements