lynnyi lynnyi - 22 days ago 10
R Question

How to make specific table in R?

Given

x <- c('typeA', 'typeB', 'typeC')
and a number
n
,
I want to make a table:

type file path_to_file
typeA typeA_1 /home/lynnyi/typeA/typeA_1.txt
typeA typeA_2 /home/lynnyi/typeA/typeA_2.txt
...
typeA typeA_n /home/lynnyi/typeA/typeA_3.txt
typeB typeB_1 /home/lynnyi/typeB/typeB_1.txt
...
typeB typeB_n /home/lynnyi/typeB/typeB_1.txt
typeC typeC_1 /home/lynnyi/typeC/typeB_1.txt
...
typeC typeC_n /home/lynnyi/typeC/typeB_1.txt


I know how to use various sapplys to get the value of each of the columns, i.e.
['typeA_1',...,'typeA_n',.......,'typeC_n']
and
['/home/lynnyi/typeA/typeA_1.txt', .... ... , '/home/lynnyi/typeC/typeC_n.txt']


But then I lose the ability to join them based in type and num...

How do I have them all in one table / data frame?

Thanks!

ira ira
Answer

If n is same for all types, this can be done as follows:

x <- c('typeA', 'typeB', 'typeC')
n <- 10

types <- sort(rep(x, 10))
files <- paste(types, rep(1:n, 3), sep = "_")
paths <- paste("/homme/lynnyi", types, paste0(files, ".txt"), sep = "/")

data <- cbind(types, files, paths)
Comments