In R Data Reshaping is about to organized data into row and columns. Data reshaping is widely used because it takes less time to extract data from rows and column.Their are many functions available in r. i.e split ,merge, melting and casting.
- Join rows and columns in data frame.
Cbind() function is used to join multiple vectors to create data frame. Using rbind() function we can merge two or more data frame.
For Example:
We can create two vector as city and state. we can combine two vector into one data frame. i.e(address)
> city<-c("mumbai","pune","delhi","hyderabad")
> state<-c("maharashtra","maharashtra","haryana","telangana")
> address<-cbind(city,state)
> print(address)
it will shows following result,
city state
[1,] "mumbai" "maharashtra"
[2,] "pune" "maharashtra"
[3,] "delhi" "haryana"
[4,] "hyderabad" "telangana"
Again ,we can create one new dataframe with the same columns.
>new.address<data.frame(city=c("nanded","latur","noida"),state=c("maharashtra","maharashtra","uttar pradesh"))
> print(new.address)
It will produce following outut:
city state
1 nanded maharashtra
2 latur maharashtra
3 noida uttar pradesh
Combine rows from both the data frame using rbind().
> all.address<-rbind(address,new.address)
> print(all.address)
When we execute the code it will generate following result .
city state
1 mumbai maharashtra
2 pune maharashtra
3 delhi haryana
4 hyderabad telangana
5 nanded maharashtra
6 latur maharashtra
7 noida uttar pradesh