I am trying to reverse the rows of one column of a dataframe by group, where grouping is driven by multiple factor columns.
I would like to go from here
I would like to reverse numeric values of Col5 according to selected groups, so as to obtain this:
All the other columns are factor
I tried with this code, but it failed
reord<-a %>% dplyr::group_by(Col1,Col2,Col4)%>% dplyr::mutate(reve=rev(Col5))
Do you have any suggestion? Any help is highly appreciated Thanks in advance
Made it.
Here my solution (also used some code from forums, so thanks anyway).
split<-a %>% ##a is the original dataframe
group_split(Col1, Col2, Col4) ## the dataframe is subset to tibble
split1<-lapply(split, function(x) dplyr::mutate(x, reve=rev(Col5))) ## rev() is applied to all columns of interest of the list
reord1<-bind_rows(split1, .id = 'id') ## the list objects are rbound to obtain the dataframe with either original values in Col5 and reverse values in the new column "reve"
It worked well
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.