nickolakis
nickolakis

Reputation: 621

insert arguments in a vector's row in R

I have a matrix 100x100 that i converted it in a singular vector variable 10000x1 named K in R, created from a list as follows:

K_o <- unlist(Mist$K)
K <- as.double(format(round(K_o, 10), nsmall = 10) #keep the first ten decimals and read it as numeric

I want to store it in a .txt file that has the following arguments: first row: the lenght of a list length(list) which is 5 in my case, second row: K 100 100 where, K is the name of the original matrix/vector,and 100, 100 are the nrow(K) and ncol(K) of the original matrix. The final .txt i want to look like this:

5
K 100 100
2.1840028592
6.3920476950
45.9584730261
3.7877586843
.
.
.

How do i manage to do that? Can someone help me out?

Upvotes: 1

Views: 40

Answers (1)

Ronak Shah
Ronak Shah

Reputation: 388807

You can try :

K_o <- Mist$K
K <- as.double(format(round(K_o, 10), nsmall = 10))
writeLines(paste(length(list), '\nK', nrow(K_o), ncol(K_o), 
          '\n',paste0(K, collapse = "\n")), 'data.txt')

Upvotes: 1

Related Questions