I'm new to R and have a problem with manipulating data in R. I combined multiple excel files and then separate a string column into multiple string columns. I found that the contents in original columns are not in the same order. How can I select some specific rows to work with and then rearrange some columns in specific rows into the correct order?
The example of dataframe as shown below. I would like to rearrange row#4 and row#5.
Thank you very much.
> a <- c("I", "You", "We", "eat", "eat")
> b <- c("eat", "eat", "eat", "They", "We")
> df <- data.frame(a, b)
> df
> df
a b
1 I eat
2 You eat
3 We eat
4 eat They
5 eat We
Try this,we can use transform function
a <- c("I", "You", "We", "eat", "eat")
b <- c("eat", "eat", "eat", "They", "We")
df <- data.frame(a, b)
df<-rbind(df[1:3,],transform(df[4:5,],a=b,b=a))
output
a b
1 I eat
2 You eat
3 We eat
4 They eat
5 We eat
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.