Changing structure of given data

I have been given external data organized randomly in 6 rows and 18 columns, however each number corresponder to an observation of individuals, for that reason I would like to change the format to just two columns where one of them is called "individuals" and the other "level" (observations).

In the screenshot, you can see how the given data in .txt format looks like and how I want it to look in R (one column individuals and in the other observations):

When I created the data, I do not how to change the structure.
I am new using R Studio, and I have not found an answer for my doubt in the forum.

The following may help

library(tidyverse)

# #external_data.txt
# 126 124 123 157 160
# 148 155 21 154 62
# 15 20

tbbl <- read_delim("external_data.txt", col_names= FALSE, delim = " " )

# 
#     # A tibble: 3 x 5
#     X1    X2    X3    X4    X5
#     <dbl> <dbl> <dbl> <dbl> <dbl>
# 1   126   124   123   157   160
# 2   148   155    21   154    62
# 3    15    20    NA    NA    NA


tbbl %>% 
  pivot_longer(cols = everything(), values_to = "observations") %>% 
  mutate(individuals = row_number()) %>% 
  select(individuals, observations) %>% 
  drop_na()


This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.