Why write.fwf() did not follow the fixed width set - r

I want to write a set of randomly generated numbers to a text file with fixed format. But for some reasons, write.fwf only wrote the 1st column right, all other columns got one extra digit. How can I fix it? Thanks!
set.seed(1899)
library(sensitivity)
library(randtoolbox)
par_lower <- c( 0.12, 0.13, 0.038, 0.017)
par_upper <- c(12.00, 13.00, 3.800, 1.700)
sample_size <- 5
lim_para8 <- c(par_lower[1], par_upper[1])
lim_para9 <- c(par_lower[2], par_upper[2])
lim_parb8 <- c(par_lower[3], par_upper[3])
lim_parb9 <- c(par_lower[4], par_upper[4])
par_rand <- parameterSets(par.ranges = list(lim_para8, lim_para9,
lim_parb8, lim_parb9),
samples = sample_size, method = "sobol")
par_rand
# write to file
library(gdata)
file2write <- paste("par.txt", sep = "")
write.fwf(par_rand, file = file2write, width = c(10, 10, 10, 10), colnames = FALSE)
The results:
6.060 6.56500 1.91900 0.858500
9.030 3.34750 2.85950 0.437750
3.090 9.78250 0.97850 1.279250
4.575 4.95625 2.38925 0.227375
10.515 11.39125 0.50825 1.068875
If I changed to
write.fwf(par_rand, file = file2write, width = c(10, 9, 9, 9),
colnames = FALSE, quote = FALSE, rownames = FALSE)
I got this error
Error in write.fwf(par_rand, file = file2write, width = c(10, 9, 9, 9), :
'width' (9) was too small for columns: V4
'width' should be at least (10)

Please try the code below, it works for me. I tested with several formats and all worked. Both code segments return a fixed format file with width 4 x 10.
This of course implies that setting sep in the definition of file2write does not work for getting the desired output with write.fwf
write.fwf(par_rand, file = "par2.txt", width = c(10, 10, 10, 10), colnames = FALSE, sep = "")
write.fwf(par_rand, file = file2write, width = c(10, 10, 10, 10), colnames = FALSE, sep = "")
The following generates the same but with 1x10 and 3x9, as I think you wanted
write.fwf(par_rand, file = "par3.txt", width = c(10, 9, 9, 9), colnames = FALSE, sep = "")
Please let me know whether this is what you wanted.

Related

Change Size of Additional Flextable Header

I have been using add_header_lines() to add a header to my flextable. I would like this header to have a different text format than the table as a whole. Unfortunately, when I use fontsize() it formats text for both the header and the column names. Is there a way to only change the text format for the header produced with add_header_lines()?
Here is some sample code:
df <- data.frame(crime = c("assault", "homicide", "burglary"),
Jan = c(5, 2, 7),
Feb = c(2, 4, 0),
Mar = c(1, 2, 1))
flex <- flextable(df)
flex <- add_header_lines(flex, values = "This is a header")
flex <- fontsize(flex, size = 15, part = "header")
As you can see, the fontsize function affects both the added header and the column values. Any idea of how to change just the headers text format?
You have to use the selector i.
library(flextable)
df <- data.frame(crime = c("assault", "homicide", "burglary"),
Jan = c(5, 2, 7),
Feb = c(2, 4, 0),
Mar = c(1, 2, 1))
flex <- flextable(df)
flex <- add_header_lines(flex, values = "This is a header")
flex <- fontsize(flex, size = 15, part = "header", i = 1)
flex <- color(flex, color = "red", part = "header", i = 1)
flex

Parts Missing From Plot, That then Reappear and Overwrite The Entire Plot When Saved? (R, Heatmap.2)

I'm using heatmap.2 to create a plot, however, the initial plot that is saved to my source folder is missing a key and title.
When I then run the dev.off() command, the Key and the Title are then used to overwrite the original graph?
For instance, I will produce a plot like this:
Which is far from perfect. But then when I run the dev.off() to close the device (otherwise a host of other errors ensue):
What you are looking at above is a very distorted Key and my 'XYZ' title.
Why on earth is it creating two files, firstly the one with my matrix, and then overwriting this with a second file containing my flipping key and my title? I cannot follow the logic.
I've updated my OS, my version of R, RStudio, all my packages and unistalled RStudio. Nothing seems to help.
If you'd like to try and replicate my error here is the example matrix:
structure(c(1, 4, 5, 3, 3, 4, 6, 1, 7, 5, 5, 4, 4, 8, 1, 3, 9,
2, 2, 9, 3, 1, 3, 4, 4, 5, 5, 5, 1, 4, 4, 3, 3, 3, 9, 1), .Dim = c(6L,
6L))
And this is the script I'm using to plot my example data. You'll need to provide a SourceDir and make sure you assign the matrix to the name "Matrix".
if (!require("gplots")) {
install.packages("gplots", dependencies = TRUE)
library(gplots)
}
if (!require("RColorBrewer")) {
install.packages("RColorBrewer", dependencies = TRUE)
library(RColorBrewer)
}
my_palette <- colorRampPalette(c("snow", "yellow", "darkorange", "red"))(n = 399)
transition
col_breaks = c(seq(0,1,length=100), #white 'snow'
seq(2,4,length=100), # for yellow
seq(5,7,length=100), # for orange 'darkorange'
seq(8,9,length=100)) # for red
png(paste(SourceDir, "Heatmap_Test.png"),
width = 5*1000,
height = 5*1000,
res = 300,
pointsize =15)
heatmap.2(Matrix,
main = paste("XYZ"),
notecol="black",
key = "true" ,
colsep = c(3, 6, 9),
rowsep = c(3, 6, 9),
labCol = NULL,
labRow = NULL,
sepcolor="white",
sepwidth=c(0.08,0.08),
density.info="none",
trace="none",
margins=c(1,1),
col=my_palette,
breaks=col_breaks,
dendrogram="none",
RowSideColors = c(rep("blue", 3), rep("orange", 3)),
ColSideColors = c(rep("blue", 3), rep("orange", 3)),
srtCol = 0 ,
asp = 1 ,
adjCol = c(NA, 0) ,
adjRow = c(0, NA) ,
#keysize = 2 ,
Colv = FALSE ,
Rowv = FALSE ,
key.xlab = paste("Correlation") ,
cexRow = (1.8) ,
cexCol = (1.8) ,
notecex = (1.5) ,
lmat = rbind(c(0,0,0,0), c(0,0,2,0),c(0,1,3,0),c(0,0,0,0)) ,
#par(ColSideColors = c(2,2)),
lhei = c(1, 1, 3, 1) ,
lwid = c(1, 1, 3, 1))
dev.off()
I'd really appreciate any insight into this problem.
I believe this resulted from the fact that I had more than just elements 1 to four, as the coloured rows I had added counted as additional elements that had to be arranged in the display matrix.
As such:
mat = rbind(c(0,0,0,0), c(0,0,2,0),c(0,1,3,0),c(0,0,0,0)) ,
lhei = c(1, 1, 3, 1) ,
lwid = c(1, 1, 3, 1))
No longer cut the butter. After much ado, I finally managed to get the following layout to work (on my actual data, not my example data).
lmat = rbind(c(0,4,5,0), c(0,0,2,0),c(0,1,3,0),c(0,0,6,0)) ,
lhei = c(0.4, 0.16, 3, 0.4) , # Alter dimensions of display array cell heighs
lwid = c(0.4, 0.16, 3, 0.4),
Notice the inclusion of elements 5 and 6.
So my final command looks like this (note that there will be many other changes but the real progress happened once I added in 5 and 6):
png(paste(SourceDir, "XYZ.png"),
width = 5*1500,
height = 5*1500,
res = 300, # 300 pixels per inch
pointsize =30)
heatmap.2(CombinedMtx,
main = paste("XYZ"), # heat map title
notecol="black",
key = "true" ,# change font color of cell labels to black
colsep = c(6, 12, 18),
labCol = c(" "," "," ", "XX"," "," "," "," "," ", "YY"," "," "," "," "," ", "ZZ"," "," "," "," "," ", "QQ"),
rowsep = c(6, 12, 18),
labRow = c(" "," "," ", "XX"," "," "," "," "," ", "YY"," "," "," "," "," ", "ZZ"," "," "," "," "," ", "QQ"),
sepcolor="white",
sepwidth=c(0.08,0.08),
density.info="none",
trace="none",
margins=c(1,1),
col=my_palette,
breaks=col_breaks,
dendrogram="none",
RowSideColors = c(rep("#deebf7", 6), rep("#1c9099", 6), rep("#addd8e", 6), rep("#fee391", 6)),
ColSideColors = c(rep("#deebf7", 6), rep("#1c9099", 6), rep("#addd8e", 6), rep("#fee391", 6)),
srtCol = 0 ,
asp = 1 ,
adjCol = c(1.5, -61.5) ,
adjRow = c(0, -1.38),
offsetRow = (-59.5),
keysize = 2 ,
Colv = FALSE ,
Rowv = FALSE ,
key.xlab = NA ,
key.ylab = NULL ,
key.title = NA ,
cexRow = (1.6) ,
cexCol = (1.6) ,
notecex = (1.5) ,
cex.main = (20),
lmat = rbind(c(0,4,5,0), c(0,0,2,0),c(0,1,3,0),c(0,0,6,0)) ,
#par(ColSideColors = c(2,2)),
lhei = c(0.4, 0.16, 3, 0.4) , # Alter dimensions of display array cell heighs
lwid = c(0.4, 0.16, 3, 0.4),
symkey = any(0.5 < 0, na.rm=FALSE) || col_breaks,
key.par=list(mar=c(3.5,0, 1.8,0) ) #tweak specific key paramters
)
dev.off()
Also, if you don't start each time by creating the PNG and enf each time by using dev.off() it won't work. I believe this might also have been contribution to my confusion, and potentially after drawing the heatmap, some elements were being drawn once the dev.off() command was run, causing the heatmap to be overwritten.
This (with my matrix) creates this image.
What I have done is a really gammy way of labelling my blocks but until I can work out how to get ComplexHeatmap working properly I'll be stuck using hacks like this with Heatmap.2.

print multiple field from an R dataframe beside eachother

I would like my output to be print as each field beside each-other instead of all the values for the first field followed by all the values for the second
This is my script
clothing = c('coat_1','coat_1','coat_1','coat_2','coat_2','coat_2','scarf','scarf','scarf')
freq = c(0.3, 0.9, 0.1, 0.5, 0.3, 0.1, 0.4, 0.2, 0.1)
temp = c(10, 0, 20, 20, 15, 5, 30, 20, 10)
#exn = c(1, 1, 1, 2, 2, 2, 1, 1, 1)
df <- data.frame(clothing, freq, temp)
print(c(toString(df$clothing[df$temp>=20]), toString(df$temp[df$temp>=20])))
I get
[1] "coat_1, coat_2, scarf, scarf" "20, 20, 30, 20"
As output but I would like it output as
coot_1 20
coat_2 20
scarf 30
scarf 20
#Prepare your text
mytext = paste(paste(df$clothing[df$temp>=20], df$temp[df$temp>=20]), collapse = "\n")
#Display in console
cat(mytext)
#coat_1 20
#coat_2 20
#scarf 30
#scarf 20
#Write to a file
cat(mytext, file = "~/test.txt")
cat can also append to an existing file. You may need to add a line break at the end of the previous text first
#Note this time I added a line break ("\n") at the end
mytext = paste0(paste(paste(df$clothing[df$temp>=20], df$temp[df$temp>=20]),
collapse = "\n"),"\n")
cat(mytext, file = "~/test.txt") #Check the output file
cat(mytext, file = "~/test.txt", append = TRUE) #Check the output file again
Edited to include step to output to file...
df.print <- df[which(df$temp>=20),c('clothing', 'temp')]
print(df.print)
write.csv(df.print, file = "c:\\myname\\yourfile.csv")

ddply to ksmooth function

I have a data frame with several columns. the relevant three are chr, pos and ratio. I want to use ddply to ksmooth based on chr (chromosome) but keep getting a wrong data frame with lots of NA values. Here is my reproducible data frame:
d=data.frame(chr=c(rep.int(1,24),rep.int(2,15),rep.int(3,30),rep.int(4,20),rep.int(5,11)),
pos=c(sort(sample(1:1000, size = 24, replace = FALSE),decreasing = FALSE), sort(sample(1:1000, size = 15, replace = FALSE),decreasing = FALSE), sort(sample(1:1000, size = 30, replace = FALSE),decreasing = FALSE), sort(sample(1:1000, size = 20, replace = FALSE),decreasing = FALSE), sort(sample(1:1000, size = 11, replace = FALSE),decreasing = FALSE)),
ratio=seq(1:100))
and ddply function
f <- ddply(d, .(chr),
function(e) {
as.data.frame(ksmooth(e$pos,e$ratio,"normal",bandwidth=10))
})
Obviously I'm doing something wrong.
Thanks for the help,
Guy
This is nothing related to plyr::ddply. The issue is with ksmooth. You want:
ksmooth(e$pos, e$ratio, "normal", bandwidth=10, x.points = e$pos)
Read ?ksmooth for what x.points means. By default, this is NULL, and ksmooth will use n.points instead. This is the source of all your trouble.

multiple files for reading and saving

I have multiple files to read and also do heatmap after which the outputs will be saved. Somehow, there is a problem(s) with my code below and I can't figure out why it is not working. Files: mxn.dat, scu.dat, emun.dat, ser.dat
files <- list.files(pattern=".dat")
for (i in length(files)){
data <-read.table(files[i],row.names=1,header=T,sep='\t')
for in length(files){
png('i.png')
pheatmap(t(data[i]), cellwidth = 32, cellheight = 14, fontsize = 5, show_colnames = T, cluster_cols = FALSE)
dev.off()
}
}
Any help will be appreciated to get the code working.
Thanks
Rob
Not a reproducible example so I have no way to test if this will work (and somehow I think that this is too localised to be useful to future visitors), but perhaps try this...
files <- list.files(pattern=".dat")
for (i in 1:length(files)){
data <-read.table( files[i],row.names=1,header=T,sep='\t')
png( paste0( i , '.png' ) )
pheatmap( t( data ), cellwidth = 32, cellheight = 14, fontsize = 5, show_colnames = T, cluster_cols = FALSE)
dev.off()
}
Variation of SimonO101's solution:
files <- list.files(pattern=".dat")
for( f in files )
{
data <-read.table( f, row.names = TRUE, header = TRUE, sep = '\t' )
png( gsub( "pdf", "png", f ) )
pheatmap( t( data ), cellwidth = 32, cellheight = 14, fontsize = 5, show_colnames = T, cluster_cols = FALSE)
dev.off()
}
Easier to read (I believe) and has the advantage (?) of preserving the original file names, only changing the extension.

Resources