On Feb 3, 2010, at 7:09 AM, Amelia Livington wrote:

Dear R helpers

After executing the R code, where the last few lines of the code are something like given below.


## Part of my R code


n = 20

........
.........

final_output = data.frame(Numbers = numbers, ABC = data1, XYZ = data2, PQR = data3)

write.csv(data.frame(Scenario = paste("Sc_", 1:n, sep = ""), final_output'), 'result.csv', row.names = FALSE)

## End of code


When I open the 'result.csv' file, my output is like

Scenario Numbers ABC XYZ PQR Sc_1 1 22 18 6 Sc_2 2 24 16.5 11 SC_3 3 38 41 38

................................................................

Sc_20 20 15 27.5 74


This is done for the first three lines of data provided, but should obviously be modified to accomadate different lengths of data.

> f2 <- stack(final_output)
#You can rename the columns to suit your needs; by default they are ind and values.

> f2$Numbers <-rep(1:3,1)  # should be rep(1:20, 3) in final solution
> f2$Scenario <- paste("Sc_", f2$Numbers, sep="")
> f2
  values ind Numbers Scenario
1   22.0 ABC       1     Sc_1
2   24.0 ABC       2     Sc_2
3   38.0 ABC       3     Sc_3
4   18.0 XYZ       1     Sc_1
5   16.5 XYZ       2     Sc_2
6   41.0 XYZ       3     Sc_3
7    6.0 PQR       1     Sc_1
8   11.0 PQR       2     Sc_2
9   38.0 PQR       3     Sc_3
>> f2 <- f2[order(f2$Numbers), ]
> f2
  values ind Numbers Scenario
1   22.0 ABC       1     Sc_1
4   18.0 XYZ       1     Sc_1
7    6.0 PQR       1     Sc_1
2   24.0 ABC       2     Sc_2
5   16.5 XYZ       2     Sc_2
8   11.0 PQR       2     Sc_2
3   38.0 ABC       3     Sc_3
6   41.0 XYZ       3     Sc_3
9   38.0 PQR       3     Sc_3
> write.csv( f2[ , c(4,3,2,1) ], file='result.csv', row.names=FALSE)

(Or write.csv( f2[ , c("Scenario","Numbers","ind","values") ], file='result.csv', row.names=FALSE)
----
"Scenario","Numbers","ind","values"
"Sc_1",1,"ABC",22
"Sc_1",1,"XYZ",18
"Sc_1",1,"PQR",6
"Sc_2",2,"ABC",24
"Sc_2",2,"XYZ",16.5
"Sc_2",2,"PQR",11
"Sc_3",3,"ABC",38
"Sc_3",3,"XYZ",41
"Sc_3",3,"PQR",38

You said to use write.csv which does put the commas in despite yuor example htat was comma-less.



## MY REQUIEMENT

I wish to have output like given below.

Scenario             Numbers       Names      Values

Sc_1                        1             ABC         22
Sc_1                        1             XYZ          18
Sc_1                        1             PQR           6
Sc_2                        2             ABC          24
Sc_2                        2             XYZ          16.5
Sc_2                        2             PQR           11

.................................................................


Sc_20                     20           ABC           15
Sc_20                     20           XYZ            27.5
Sc_20                     20           PQR           74


Please guide how this can be achieved?

Regards and thanks in advance

Amelia








        [[alternative HTML version deleted]]

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

David Winsemius, MD
Heritage Laboratories
West Hartford, CT

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to