[R] Matrix in a Reverse order
Dear 'R' friends I have a sort of stupid question to ask. I have a matrix say of the order 4 X 3 as 83 98 90 21 83 84 70 39 56 65 29 38 Is there any command in R which will reverse the order i.e. I need to have same 4 X 3 matrix but as given below 65 29 38 70 39 56 21 83 84 83 98 90 i.e. the last row becomes first row, second - last row becomes second row and so on. I need this to allign it with some other variable set. Thanks 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.
[R] how to read csv file having variables unequal column sizes
Dear R helpers Suppose e.g. I have a csv file having three variables defined and each of these variables have data items of say 40, 50, 45 length. When I open this csv file in 'R', I get 10 trailing 'NA's under first column and 5 'NA' s in case of 3rd column. How do I get rid of these NA's s.t. when I read the first column, there should be only 40 data items, 2nd column should have only 50 data items and last one should have 45 data items as in the original csv file. Thanking 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.
Re: [R] Readjusting the OUTPUT csv file
Dear Sir, I really appreciate your suggestion. I was having the probelm of expressing my problem precisely. In fact in my earlier mail, I have tried to write my probelm in my initial few paras only. The rest content is my actual R code which I have written and its working fine except for the final output which I want to have in a particular form. I had produced the said R code as I thought it may be handy for someone who may be interested. For me another option was to attach the related R code but I understand we are not supposed to send the attachments. It was a difficult for me to express my problem effectively in few lines. Please trust me that I had drafted it 2-3 times. I once again try to resend it without the related R code. I thought it may be useful to have this R code handy. But truely I sincerely apologize for the same and do appreciate the time contraints all the R helpers have. Please Forgive me. Here is my probelm restated. ### PROBLEM I have some variables say ABC, DEF, PQR, LMN and XYZ. I am choosing any three varaibles at random at a time for my analysis and name these files as input1.csv, input2.csv and input3.csv. So if I choose variables say ABC, DEF and PQR, I am passing the specifications of these variables to input1.csv, input2.csv and input3.csv respectively. This means in another case even if I choose say LMN, DEF and XYZ in this order, then my input1.csv will have details of LMN, input2.csv will have detaisl of DEF and input3.csv will have details of XYZ. Thus, I am keeping the (input) file names constant but the contents of each of these input files may change from one case to another case. As an example, my input files are as given below. input1.csv name1 value1 DEF 10 input2.csv name2 value2 LMN 8 input3.csv name3 value3 PQR 7 ## __ ## The Problem name1 = read.csv('input1.csv')$name1 value1 = read.csv('input1.csv')$value1 name2 = read.csv('input2.csv')$name2 value2 = read.csv('input2.csv')$value2 name3 = read.csv('input3.csv')$name3 value3 = read.csv('input3.csv')$value3 # (Thus if 1st variable chosen is DEF, then name1 = DEF, value1 = 10. Had I chosen some otehr variable, then name1 and value1 will have different values). Then I carry out my analysis and get the output as given below. Scenario_and_range names values Sc_1 (DEF LMN PQR) name1 10.04 Sc_1 (DEF LMN PQR) name2 8.21 Sc_1 (DEF LMN PQR) name3 7.23 Sc_2 (DEF LMN PQR) name1 9.96 Sc_2 (DEF LMN PQR) name2 8.47 Sc_2 (DEF LMN PQR) name3 7.40 . . The values appearing in the third column are the random nos. generated using the base rate 10, 8 and 7 respectively. My probelm is I know name1 pertains to DEF, name2 pertains to LMN and name3 pertains to PQR. But in the output I am not able to replace name1 with DEF, name2 with LMN and so on i.e. I cannot hardcode the varaible names in data.frame as name1 = DEF, name2 = LMN etc as these names are going to change frome case to case. My objective is to get the names (in column 2 of above table) same as respective names (appearing in 1st column) in its order i.e. instead of name1, I should get DEF, in place of name2, I should get LMN and in place of name3, I should get PQR. Hence, my output should be like - Scenario_and_range names values Sc_1 (DEF LMN PQR) DEF 10.04 Sc_1 (DEF LMN PQR) LMN 8.21 Sc_1 (DEF LMN PQR) PQR 7.23 Sc_2 (DEF LMN PQR) DEF 9.96 Sc_2 (DEF LMN PQR) LMN 8.47 Sc_2 (DEF LMN PQR) PQR 7.40 . . Regards Amelia ##_ --- On Wed, 2/10/10, Dieter Menne wrote: From: Dieter Menne Subject: Re: [R] Readjusting the OUTPUT csv file To: r-help@r-project.org Date: Wednesday, February 10, 2010, 8:02 AM Amelia wrote a few hundreds of lines This is too much reading. Please try to reduce you problem to 30 lines, and make the example self-contained (your data are not available). In 80% of the cases, the problem is self-resolved after it has been made self-contained. Dieter -- View this message in context: http://n4.nabble.com/Readjusting-the-OUTPUT-csv-file-tp1475448p1475478.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https:
[R] Readjusting the OUTPUT csv file
Dear R helpers I have some variables say ABC, DEF, PQR, LMN and XYZ. I am choosing any three varaibles at random at a time for my analysis and name these files as input1.csv, input2.csv and input3.csv. So if I choose variables say ABC, DEF and PQR, I am passing the specifications of these variables to input1.csv, input2.csv and input3.csv respectively. This means in another case even if I choose say LMN, DEF and XYZ in this order, then my input1.csv will have details of LMN, input2.csv will have detaisl of DEF and input3.csv will have details of XYZ. Thus, I am keeping the (input) file names constant but the contents of each of these input files may change from one case to another case. As an example, my input files are as given below. input1.csv name1 value1 DEF 10 input2.csv name2 value2 LMN 8 input3.csv name3 value3 PQR 7 ## __ ## The Problem name1 = read.csv('input1.csv')$name1 value1 = read.csv('input1.csv')$value1 name2 = read.csv('input2.csv')$name2 value2 = read.csv('input2.csv')$value2 name3 = read.csv('input3.csv')$name3 value3 = read.csv('input3.csv')$value3 # (Thus if 1st variable chosen is DEF, then name1 = DEF, value1 = 10. Had I chosen some otehr variable, then name1 and value1 will have different values). Then I carry out my analysis and get the output as given below. Scenario_and_range names values Sc_1 (DEF LMN PQR) name1 10.04 Sc_1 (DEF LMN PQR) name2 8.21 Sc_1 (DEF LMN PQR) name3 7.23 Sc_2 (DEF LMN PQR) name1 9.96 Sc_2 (DEF LMN PQR) name2 8.47 Sc_2 (DEF LMN PQR) name3 7.40 . . My probelm is I know name1 pertains to DEF, name2 pertains to LMN and name3 pertains to PQR. But in the output I am not able to replace name1 with DEF, name2 with LMN and so on i.e. I cannot hardcode the varaible names in data.frame as name1 = DEF, name2 = LMN etc as these names are going to change frome case to case. My objective is to get the names (in column 2 of above table) same as respective names (appearing in 1st column) in its order i.e. instead of name1, I should get DEF, in place of name2, I should get LMN and in place of name3, I should get PQR. Hence, my output should be like - Scenario_and_range names values Sc_1 (DEF LMN PQR) DEF 10.04 Sc_1 (DEF LMN PQR) LMN 8.21 Sc_1 (DEF LMN PQR) PQR 7.23 Sc_2 (DEF LMN PQR) DEF 9.96 Sc_2 (DEF LMN PQR) LMN 8.47 Sc_2 (DEF LMN PQR) PQR 7.40 . . I have tried to expalin the problem but I am not sure how far I have been clear in that. I am giving below the actual R code I have used to arrive at the required output. Actually I have say 8-9 rates and I am choosing say any three out of them. Each of these rates have three possible ranges with respective probabilities. My objective is to generate random numbers for the various possible range COMBINATIONS in proportion to the respective joint probabilities. I have alraedy written the R code, I just need to refine my output. I also sincerely apologize for writing such a long mail. Regards Amelia My R code ** ## Actualy I am dealing with more than 3 variables i.e. rates and thus my names are different in actual code. ## Beginning of a R code ## FUNCTION NO. 3 library(reshape) no_rate = 3 combi_3 = function(n, N, rateA, rate_name1, rateA_rf1, rateA_rf2, rateA_rf3, rateAprob1, rateAprob2, rateAprob3, rateB, rate_name2, rateB_rf1, rateB_rf2, rateB_rf3, rateBprob1, rateBprob2, rateBprob3, rateC, rate_name3, rateC_rf1, rateC_rf2, rateC_rf3, rateCprob1, rateCprob2, rateCprob3) { rateA_prob1 = rateAprob3/2 rateA_prob2 = rateAprob2/2 rateA_prob3 = rateAprob1 rateA_prob4 = rateA_prob2 rateA_prob5 = rateA_prob1 rateA_ran1_min = rateA-rateA_rf3 rateA_ran1_max = rateA-rateA_rf2 rateA_ran2_min = rateA-rateA_rf2 rateA_ran2_max = rateA-rateA_rf1 rateA_ran3_min = rateA-rateA_rf1 rateA_ran3_max = rateA+rateA_rf1 rateA_ran4_min = rateA+rateA_rf1 rateA_ran4_max = rateA+rateA_rf2 rateA_ran5_min = rateA+rateA_rf2 rateA_ran5_max = rateA+rateA_rf3 rateB_prob1 = rateBprob3/2 rateB_prob2 = rateBprob2/2 rateB_prob3 = rateBprob1 rateB_prob4 = rateB_prob2 rateB_prob5 = rateB_prob1 rateB_ran1_min = rateB-rateB_rf3 rateB_ran1_max = rateB-rateB_rf2
[R] How to change output 'csv' file
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_11 22 18 6 Sc_22 24 16.5 11 SC_3 3 38 41 38 Sc_20 20 15 27.5 74 ## MY REQUIEMENT I wish to have output like given below. Scenario Numbers Names Values Sc_1 1 ABC 22 Sc_11 XYZ 18 Sc_1 1 PQR 6 Sc_22 ABC 24 Sc_22 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.
[R] Mean, median and other moments
Hi! Suppose I have a dataset as follows pd = c(10,7,10,11,7,11,7,6,8,3,12,7,7,10,10) I wish to calculate the mean, standard deviation, median, skewness and kurtosis i.e. regular standard statistical measures. average = mean(pd) stdev = sd(pd) median = median(pd) skew = skewness(pd) kurt = kurtosis(pd) Q. No (1) How do I get these at a stretch using some R package? I came across moments and e1071 package, but I am not sure which one to use and how? Q. No. (2) Many times I came across the command set.seed(1234) What is the significance of this command. I understand this is related to random number generation. But what does it do? Thanking 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.
[R] Changing Column names in (Output) csv file
Dear R helpers Following is a part of R code. data_lab <- expand.grid(c("R11", "R12", "R13"), c("R21", "R22", "R23"), c("R31", "R32", "R33"), c("R41", "R42", "R43"), c("R51", "R52", "R53"), c("R61", "R62", "R63"), c("R71", "R72", "R73"), c("R81", "R82", "R83"), c("R91", "R92", "R93"), c("R101", "R102", "R103")) range_prob <- list() range_prob[[1]] <- c(0.42,0.22,0.36) range_prob[[2]] <- c(0.14,0.56,0.30) range_prob[[3]] <- c(0.61,0.38,0.01) range_prob[[4]] <- c(0.34,0.37,0.29) range_prob[[5]] <- c(0.09,0.19,0.72) range_prob[[6]] <- c(0.42,0.21,0.37) range_prob[[7]] <- c(0.44,0.07,0.49) range_prob[[8]] <- c(0.54,0.06,0.40) range_prob[[9]] <- c(0.26,0.62,0.12) range_prob[[10]] <- c(0.65,0.19,0.16) pdf <- expand.grid(range_prob) data_lab$probs <- apply(pdf, 1, prod) joint_probs = xtabs(probs ~ Var1 + Var2+Var3+Var4+Var5+Var6+Var7+Var8+Var9+Var10, data = data_lab) write.csv(data.frame(joint_probs), 'joint_probs.csv', row.names = FALSE) ONS = read.csv('joint_probs.csv') Names = NULL for (i in 1:length(joint_probs)) { Names[i] = paste(ONS$Var1[i], ONS$Var2[i], ONS$Var3[i], ONS$Var4[i], ONS$Var5[i], ONS$Var6[i], ONS$Var7[i], ONS$Var8[i],ONS$Var9[i], ONS$Var10[i]) } write.csv(data.frame(labels = Names), 'Names.csv', row.names = FALSE) result = data.frame(read.csv('Names.csv')$labels, read.csv('joint_probs.csv')$Freq) write.csv(data.frame(result), 'prob_table.csv', row.names = FALSE) # The PROBLEM When I open the prob_table.csv file in Excel, instead of having column names as lables and Freq, I get the column heads as read.csv..Names.csv...labels read.csv..joint_probs.csv...Freq R11 R21 R31 R41 R51 R61 R71 R81 R91 R101 1.85E - 5 and so on. Ideally I will like to have the column names as Label Probability Please guide 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.
[R] Combinations
Dear R helpers, I am working on the scenario analysis pertaining to various interest rates. In this connection I need to form the various combinations as under : Suppose I have two sets A = (a, b, c) and B = (x,y,z) Then I can easily form the cominations as (ax, ay, az, bx, by, bz, cx, cy, cz) However, if I have say 5 variables, then total no of possible combinations will be 3^5 = 243. Thus, A = (a,b,c), B = (x, y, z), C = (l, m, n), D = (p,q,r), E = (s, t, u). Then may be my possble combination will start as (a, x, l, p, s), then next combination may be (a, x, l, p, u) and so on. The last combination (243rd in this case) may be (c, z, n, r, u) or something like this. In R, is there any way to list all these 3^5 = 243 combinations? 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.
[R] Combinations and joint probabilities
 Dear R helpers  Suppose I have two sets of ranges (interest rates) as  Range 1 : (7 â 7.50, 7.50 â 8.50, 8.50 â 10.00) with respective probabilities 0.42, 0.22 and 0.36.   Range II : (11-12, 12-14, 14-21) with respective probabilities 0.14, 0.56 and 0.30 respectively.   My problem is to form the combinations of these ranges in a decreasing order of joint probabilities. It is assumed that these ranges are independent.  Suppose A represents (7-7.50), B represents (7.50-8.50) and C represents (8.50 â 10.00). Also let X be (11-12), Y is (12-14) and Z is (14-21).                        These two groups are independent i.e. Prob(A and Y) = P(A) * P(Y)  So there are 9 combinations possible as (AX, AY, AZ, BX, BY, BZ, CX, CY and CZ) respectively with the joint probabilities (0.059, 0.235, 0.126, 0.031, 0.123, 0.066, 0.05, 0.202, 0.108) respectively.  My problem is (i)                              How to obtain these 9 combinations of probabilities in the sense how do I obtain the various combinations of these two ranges along-with their respective probabilities; (ii)                            How to arrange these 9 probabilities in descending order against the respective group combination i.e. for the combination AY, the joint probability is maximum at 0.235, followed by CY at 0.202 and so on.  I sincerely apologize as perhaps I might not have raised the query properly. I have become member of this group today only and its been hardly a week since I have started learning R language. I have easily done this in Excel. My output should be something like this â  Combination  Probability AY                  0.235 CY                  0.202 AZ                 0.126 BY                  0.123 CZ                 0.108 BZ                 0.066 AX                 0.059 CX                 0.050 BX                 0.031   I request you to guide me.  Thanking 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.