In data frame how to spilt strings into values?

0 votes

Here is a data frame which consist of character variable "SAP"

 ID                  VAR
1  4 SAP1\r\nSAP2\r\nSAP8
2  5         SAP2\r\nSAP5

I would like to split the variable -\r\n and obtain the desired dataframe:

  ID  VAR
1  4 SAP1
2  4 SAP2
3  4 SAP8
4  5 SAP2
5  5 SAP5
Apr 4, 2018 in Data Analytics by DataKing99
• 8,100 points
37 views

2 answers to this question.

0 votes


We can easily do this with separate_rows() function

library(tidyr)
separate_rows(df, VAR, sep='\\s+')
answered Apr 4, 2018 by DeepCoder786
• 1,700 points
0 votes

You can do this using dplyr and tidyr:

library(dplyr)
library(tidyr)
v %>% 
  mutate(string_name = strsplit(as.character(string_name), ",")) %>%
  unnest(string_name)
answered Dec 4, 2018 by Kalgi
• 37,320 points

Related Questions In Data Analytics

0 votes
1 answer

How to remove rows with missing values (NAs) in a data frame?

You can use complete.cases in the following ...READ MORE

answered Apr 13, 2018 in Data Analytics by darklord
• 6,140 points
3,685 views
0 votes
1 answer
0 votes
1 answer

How to convert a list to data frame in R?

Let's assume your list of lists is ...READ MORE

answered Apr 12, 2018 in Data Analytics by nirvana
• 3,060 points

edited Apr 12, 2018 by nirvana 1,839 views
0 votes
1 answer

How to drop factor levels in a subsetted data frame?

You can use factor(ff) to drop levels ...READ MORE

answered Apr 17, 2018 in Data Analytics by kappa3010
• 2,010 points

edited Apr 17, 2018 by kappa3010 688 views
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

How to sort a data frame by columns in R?

You can just use the order function ...READ MORE

answered Apr 10, 2018 in Data Analytics by darklord
• 6,140 points
73 views
0 votes
1 answer
0 votes
1 answer

How to spilt a column of a data frame into multiple columns

it is easily achievable by using "stringr" ...READ MORE

answered Apr 9, 2018 in Data Analytics by DeepCoder786
• 1,700 points
52 views
0 votes
1 answer

How to convert a list of vectors with various length into a Data.Frame?

We can easily use this command as.data.frame(lapply(d1, "length< ...READ MORE

answered Apr 4, 2018 in Data Analytics by DeepCoder786
• 1,700 points
46 views