I have a dataset that has 148 columns. I need to combine them in groups of 4. For example V1,V2,V3,V4 =X1
V1 <-c(0,3)
V2 <-c(F,F)
V3 <-c (2,4)
V4 <-c(A,C)
X1
0F2A
3F4C
I know I can use
```{r}
new_data_4 <-new%>%
unite(V1:V4)%>%
unite(V5:V8)%>%
unite(V9:V12)%>%
unite(V13:V16)
```
with great success but I would like to make this function. My wish is that it can count the number of columns and do it automatically without hardcoding the numbers. I have more files to go over. I have looked around StackOverFlow and found LOTS of examples with specific problems that don't really jive with what I have.
I have tried:
```{r}
unite_columns <-function(x){
united_cols <-tidyr::unite(x, seq_along(1, ncol(x), 4), seq_along(4, ncol(x), 4))
return(united_cols)
}
```
and
```{r}
unite_columns <-function(x){
united_cols <-unite(x, seq(1, ncol(x), 4), seq(4, ncol(x), 4))
return(united_cols)
}
```
I was thinking I could use a similar tactic that is used to merge strings but it did not work. Any help would be greatly appreciated. TIA