0

suppose, i have such files

rock=structure(list(x1 = c(0, 0.8, 0.4, 0.3, 0.5, 1, 0.7, 0.6, 0.4, 
0.4, 0.6), x2 = c(0, 1, 0.5, 0.3, 0.5, 0.5, 0.8, 0.3, 0.6, 0.8, 
0.7), x3 = c(0, 0.4, 0.8, 0.4, 0.2, 1, 0.5, 0.8, 0.4, 1, 0.3), 
    x4 = c(0, 0.3, 0.4, 0.4, 0.5, 0.6, 0.8, 0.3, 0.7, 0.6, 0.2
    )), class = "data.frame", row.names = c(NA, -11L))
rave=structure(list(x1 = c(0, 0.8, 0.4, 0.3, 0.5, 1), x2 = c(0, 1, 
0.5, 0.3, 0.5, 0.5), x3 = c(0, 0.4, 0.8, 0.4, 0.2, 1), x4 = c(0, 
0.3, 0.4, 0.4, 0.5, 0.6)), class = "data.frame", row.names = c(NA, 
-6L))
classic=structure(list(x1 = c(0, 0.8), x2 = 0:1, x3 = c(0, 0.4), x4 = c(0, 
0.3)), class = "data.frame", row.names = c(NA, -2L))

How to do that when i rbind these datasets, for each dataset paste original name I.e the result i want to see this like this. Initial data with names in csv format. For example

classic=read.csv(path to classic.csv)

   dataset  x1  x2  x3  x4
1  classic 0.0 0.0 0.0 0.0
2  classic 0.8 1.0 0.4 0.3
3     Rave 0.0 0.0 0.0 0.0
4     Rave 0.8 1.0 0.4 0.3
5     Rave 0.4 0.5 0.8 0.4
6     Rave 0.3 0.3 0.4 0.4
7     Rave 0.5 0.5 0.2 0.5
8     rock 0.0 0.0 0.0 0.0
9     rock 0.8 1.0 0.4 0.3
10    rock 0.4 0.5 0.8 0.4
11    rock 0.3 0.3 0.4 0.4
12    rock 0.5 0.5 0.2 0.5
13    rock 1.0 0.5 1.0 0.6
14    rock 0.7 0.8 0.5 0.8
15    rock 0.6 0.3 0.8 0.3
16    rock 0.4 0.6 0.4 0.7
17    rock 0.4 0.8 1.0 0.6
18    rock 0.6 0.7 0.3 0.2
psysky
  • 3,037
  • 5
  • 28
  • 64

2 Answers2

2

Put them in a list and use bind_rows :

library(dplyr)
bind_rows(lst(rock, rave, classic), .id = 'dataset')

#   dataset  x1  x2  x3  x4
#1     rock 0.0 0.0 0.0 0.0
#2     rock 0.8 1.0 0.4 0.3
#3     rock 0.4 0.5 0.8 0.4
#4     rock 0.3 0.3 0.4 0.4
#5     rock 0.5 0.5 0.2 0.5
#6     rock 1.0 0.5 1.0 0.6
#7     rock 0.7 0.8 0.5 0.8
#8     rock 0.6 0.3 0.8 0.3
#9     rock 0.4 0.6 0.4 0.7
#10    rock 0.4 0.8 1.0 0.6
#11    rock 0.6 0.7 0.3 0.2
#12    rave 0.0 0.0 0.0 0.0
#13    rave 0.8 1.0 0.4 0.3
#14    rave 0.4 0.5 0.8 0.4
#15    rave 0.3 0.3 0.4 0.4
#16    rave 0.5 0.5 0.2 0.5
#17    rave 1.0 0.5 1.0 0.6
#18 classic 0.0 0.0 0.0 0.0
#19 classic 0.8 1.0 0.4 0.3

However, it would be better if you could read the data in a list automatically without reading them individually first.

library(dplyr)
library(purrr)

filenames <- list.files('/path/to/csv', pattern = '\\.csv', full.names = TRUE)

result <- map_df(filenames, 
                 ~read.csv(.x) %>% 
                   mutate(dataset = tools::file_path_sans_ext(basename(.x))))
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
1

you can add a column with a constant name to your datasets then rbind and then put last column to first position

classic['dataset'] = 'classic'
rave['dataset'] = 'rave'
rock['dataset'] = 'rock'
df <- rbind(classic, rave, rock)
df <- df[,c(ncol(df), 1:ncol(df)-1)]
TarJae
  • 72,363
  • 6
  • 19
  • 66