user90
user90

Reputation: 381

unlist data into single line in R

I have a array as like this

DATA01   Data03  Sam2
23.2     W.343    25
67.9     3.4      3.6
2.3

Need to convert as like this

DATA01     
23.2         
67.9           
2.3
Data03
W.343
3.4
Sam2
25
3.6

I tried with t (Transpose)

t(paste(unlist(Data), collapse =" "))    
 

but It is not solving the issue

Upvotes: 0

Views: 156

Answers (1)

Ronak Shah
Ronak Shah

Reputation: 388982

You can pass column name and value to construct such one column dataframe.

data.frame(value = unlist(mapply(function(x, y) c(y,na.omit(x)), 
                          df, names(df))), row.names = NULL)

Or using purrr :

library(purrr)

imap(df, ~c(.y, na.omit(.x))) %>% flatten_chr() %>% data.frame(value = .)

#    value
#1  DATA01
#2    23.2
#3    67.9
#4     2.3
#5  Data03
#6   W.343
#7     3.4
#8    Sam2
#9      25
#10    3.6

data

Assuming this is your data :

df <- structure(list(DATA01 = c(23.2, 67.9, 2.3), Data03 = c("W.343", 
"3.4", NA), Sam2 = c("25", "3.6", NA)), row.names = c(NA, -3L
), class = "data.frame")
df

#  DATA01 Data03 Sam2
#1   23.2  W.343   25
#2   67.9    3.4  3.6
#3    2.3   <NA> <NA>

Upvotes: 1

Related Questions