Search code examples
rdataframerow

Adjust number of data for each row in the data frame


I'm new in r and currently trying to adjust the number of data in each row in my data frame. I need this data as input in the dynamic global vegetation model: Fortran-based.

Here is my data: population data: 1 column x 22465 rows

download

My data has 1 column and 22,465 rows, 1 column contains 24 datas but I want each column to have 8 datas only, and the next 9-...n, written in the next row, and so on.

I need to reorder my data to be like this: land data: 1 column x 64799 rows

download

This problem was solved by @TarJae suggestion. Thanks.

df1 <- df %>% 
separate_rows(X1) %>% 
group_by(group_id =as.integer(gl(n(),8,n()))) %>% 
mutate(X1 = toString(X1)) %>% 
ungroup() %>% 
select(-group_id)%>% 
as.data.frame

write.table(df1,"test.txt",col.names = FALSE, row.names = FALSE)

Solution

  • One way to do this is with dyplr and tidyr:

    1. Separate rows by comma
    2. group rows of n = 8
    3. use toString same as paste ...
    library(dplyr)
    library(tidyr)
    
    df %>% 
      separate_rows(X1) %>% 
      group_by(group_id =as.integer(gl(n(),8,n()))) %>% 
      mutate(X1 = toString(X1)) %>% 
      ungroup() %>% 
      select(-group_id)
    
      X1                    
       <chr>                 
     1 0, 0, 0, 0, 0, 0, 0, 0
     2 0, 0, 0, 0, 0, 0, 0, 0
     3 0, 0, 0, 0, 0, 0, 0, 0
     4 0, 0, 0, 0, 0, 0, 0, 0
     5 0, 0, 0, 0, 0, 0, 0, 0
     6 0, 0, 0, 0, 0, 0, 0, 0
     7 0, 0, 0, 0, 0, 0, 0, 0
     8 0, 0, 0, 0, 0, 0, 0, 0
     9 0, 0, 0, 0, 0, 0, 0, 0
    10 0, 0, 0, 0, 0, 0, 0, 0
    # ... with 359,726 more rows