[R] Changing Column names in (Output) csv file
Nikhil Kaza
nikhil.list at gmail.com
Wed Dec 16 14:46:09 CET 2009
I am not sure why you are reading and writing from disk so many times?
It will degrade performance.
Also avoid loops when you can.
just use
ONS$labels <- 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(c(ONS$labels, ONS$Freq), file="prob_table.csv")
Alternately just use concatenate command in excel after you exported
the joint probs.
Nikhil
On 15 Dec 2009, at 7:53AM, Amelia Livington wrote:
>
> 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 at 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.
More information about the R-help
mailing list