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
45 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
• 39,310 points

Related Questions In Data Analytics

0 votes
2 answers

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

Hi, The below code returns rows without ...READ MORE

answered 2 days ago in Data Analytics by anonymous
• 21,150 points
4,123 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 2,201 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,020 points

edited Apr 17, 2018 by kappa3010 742 views
0 votes
1 answer
0 votes
1 answer
0 votes
2 answers

How to sort a data frame by columns in R?

You can use dplyr function arrange() like ...READ MORE

answered 1 day ago in Data Analytics by anonymous
• 21,150 points
91 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
72 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
60 views