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

0 votes

Data is in this form:

df1<-data.frame(index=c(1,2,4,6,8), drive=c("low_and_high","low_and_high_2)
index          drive
1     1   low_and_high
2     2 low_and_high_2
3     4   low_and_high
4     6 low_and_high_2

by using some function I want to generate this output:

index drive_1 drive_2
1    1    low    high
2    2    low  high_2
3    4    low    high
4    6    low  high_2

Thanks in Advance!

Apr 9, 2018 in Data Analytics by CodingByHeart77
• 3,750 points
1,773 views

1 answer to this question.

0 votes

it is easily achievable by using "stringr" library:

library(stringr)
str_split_fixed(df1$drive, "_and_", 2)

Output:
[,1]  [,2]    
[1,] "low" "high"  
[2,] "low" "high_2"
[3,] "low" "high"  
[4,] "low" "high_2"
answered Apr 9, 2018 by DeepCoder786
• 1,720 points

Related Questions In Data Analytics

0 votes
1 answer
0 votes
2 answers

How to subset rows containing NA in a chosen column of a data frame?

You can give this a try. subset(dataframe, is.na(dataframe$col2)) ...READ MORE

answered Aug 21, 2019 in Data Analytics by anonymous
• 33,030 points
10,464 views
0 votes
1 answer

How to replace all occurrences of a character in a character column in a data frame in R

If you used sub() to replace the ...READ MORE

answered Jun 29, 2019 in Data Analytics by anonymous
• 33,030 points
17,935 views
0 votes
1 answer

How to combine a list of data frames into one data frame?

Use bind_rows() from the dplyr package: bind_rows(list_of_dataframes, .id ...READ MORE

answered Dec 17, 2020 in Data Analytics by Gitika
• 65,770 points
1,159 views
0 votes
1 answer

How to convert a text mining termDocumentMatrix into excel or csv in R?

By assuming that all the values are ...READ MORE

answered Apr 5, 2018 in Data Analytics by DeepCoder786
• 1,720 points
1,918 views
0 votes
1 answer

How to create a list of Data frames?

Basically all we have to do is ...READ MORE

answered Apr 9, 2018 in Data Analytics by DeepCoder786
• 1,720 points
1,338 views
+1 vote
2 answers

How to sort a data frame by columns in R?

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

answered Aug 21, 2019 in Data Analytics by anonymous
• 33,030 points
1,810 views
0 votes
1 answer

How to replace NA values in a dataframe with Zero's ?

It is simple and easy: df1<-as.data.frame(matrix(sample(c(NA, 1:10), 100, ...READ MORE

answered Apr 10, 2018 in Data Analytics by CodingByHeart77
• 3,750 points
2,061 views
+1 vote
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,720 points
1,661 views
+1 vote
3 answers

How to change column names of a Data frame?

Hi, To change the name of a column ...READ MORE

answered Oct 30, 2020 in Data Analytics by MD
• 95,460 points
1,715 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP