Fixing RStudio's default par() settings - r

Upon starting RStudio (v0.97.551 on OSX 10.8.4) running plot(1:10, 1:10) fails with Error in plot.new() : figure margins too large. This seems unrelated to the other SO questions featuring this error message as it only happens in RStudio - R's basic GUI is unaffected.
par() yields:
> par()
$xlog
[1] FALSE
$ylog
[1] FALSE
$adj
[1] 0.5
$ann
[1] TRUE
$ask
[1] FALSE
$bg
[1] "white"
$bty
[1] "o"
$cex
[1] 1
$cex.axis
[1] 1
$cex.lab
[1] 1
$cex.main
[1] 1.2
$cex.sub
[1] 1
$cin
[1] 0.2000000 0.2666667
$col
[1] "black"
$col.axis
[1] "black"
$col.lab
[1] "black"
$col.main
[1] "black"
$col.sub
[1] "black"
$cra
[1] 14.4 19.2
$crt
[1] 0
$csi
[1] 0.2666667
$cxy
[1] 0.02915216 -0.46109510
$din
[1] 8.513889 1.875000
$err
[1] 0
$family
[1] ""
$fg
[1] "black"
$fig
[1] 0 1 0 1
$fin
[1] 8.513889 1.875000
$font
[1] 1
$font.axis
[1] 1
$font.lab
[1] 1
$font.main
[1] 2
$font.sub
[1] 1
$lab
[1] 5 5 7
$las
[1] 0
$lend
[1] "round"
$lheight
[1] 1
$ljoin
[1] "round"
$lmitre
[1] 10
$lty
[1] "solid"
$lwd
[1] 1
$mai
[1] 1.360000 1.093333 1.093333 0.560000
$mar
[1] 5.1 4.1 4.1 2.1
$mex
[1] 1
$mfcol
[1] 1 1
$mfg
[1] 1 1 1 1
$mfrow
[1] 1 1
$mgp
[1] 3 1 0
$mkh
[1] 0.001
$new
[1] FALSE
$oma
[1] 0 0 0 0
$omd
[1] 0 1 0 1
$omi
[1] 0 0 0 0
$pch
[1] 1
$pin
[1] 6.8605556 -0.5783333
$plt
[1] 0.1284176 0.9342251 0.7253333 0.4168889
$ps
[1] 16
$pty
[1] "m"
$smo
[1] 1
$srt
[1] 0
$tck
[1] NA
$tcl
[1] -0.5
$usr
[1] 0 1 0 1
$xaxp
[1] 0 1 5
$xaxs
[1] "r"
$xaxt
[1] "s"
$xpd
[1] FALSE
$yaxp
[1] 0 1 5
$yaxs
[1] "r"
$yaxt
[1] "s"
$ylbias
[1] 0.2
Setting par(mai=c(0,0,0,0)) stops the error message but messes up the plot (I think it pushes plot axes outside the viewable plot area). In any case I don't see why this should be necessary - it should plot ok without the need to customise par.
Does anyone know why this is happening, and if there's any way to fix it?

If the plot window in RStudio is too small, you will get this error.
Keeping that in mind, two options to try are:
Enlarge the plot window :-)
Try plotting with the standard graphics device (which I believe should be x11() for you).

After having set par(mai=c(0,0,0,0)), close and open a new graphical device:
par(mai=c(0,0,0,0))
dev.off()
dev.new()

Related

Extracting Spot Value from Thermal Image

I have a FLIR Image from which I want to extract "spot value". The spot value is printed on the image. I found the Thermimage package that I use in R.
This is what I get when I run this:
library(Thermimage)
flirsettings("FLIR8655.jpg", exiftoolpath = "installed", camvals = "")
$Info
$Info$ExifToolVersionNumber
[1] 10.62
$Info$FileName
[1] 8655
$Info$Directory
[1] "357-2517"
$Info$FileSize
[1] 74
$Info$FilePermissions
[1] ""
$Info$FileType
[1] ""
$Info$FileTypeExtension
[1] ""
$Info$MIMEType
[1] ""
$Info$JFIFVersion
[1] 1.01
$Info$ExifByteOrder
[1] "-"
$Info$Make
[1] ""
$Info$CameraModelName
[1] 3
$Info$Orientation
[1] ""
$Info$XResolution
[1] 72
$Info$YResolution
[1] 72
$Info$ResolutionUnit
[1] ""
$Info$Software
[1] "3.6.0"
$Info$YCbCrPositioning
[1] ""
$Info$ExposureTime
[1] 150
$Info$ExifVersion
[1] 220
$Info$ComponentsConfiguration
[1] "-"
$Info$SubjectDistance
[1] 1
$Info$FocalLength
[1] 1.8
$Info$ImageTemperatureMax
[1] 311
$Info$ImageTemperatureMin
[1] 296
$Info$FlashpixVersion
[1] 100
$Info$ColorSpace
[1] ""
$Info$ExifImageWidth
[1] 320
$Info$ExifImageHeight
[1] 240
$Info$DigitalZoomRatio
[1] 1
$Info$ImageUniqueID
[1] 7.415218e+19
$Info$Compression
[1] "-"
$Info$ThumbnailOffset
[1] 1894
$Info$ThumbnailLength
[1] 2474
$Info$CreatorSoftware
[1] NA
$Info$Emissivity
[1] 0.95
$Info$ObjectDistance
[1] 1
$Info$ReflectedApparentTemperature
[1] 20
$Info$AtmosphericTemperature
[1] 20
$Info$IRWindowTemperature
[1] 20
$Info$IRWindowTransmission
[1] 1
$Info$RelativeHumidity
[1] 50
$Info$PlanckR1
[1] 11326.43
$Info$PlanckB
[1] 1316.8
$Info$PlanckF
[1] 1.65
$Info$AtmosphericTransAlpha1
[1] 0.006569
$Info$AtmosphericTransAlpha2
[1] 0.01262
$Info$AtmosphericTransBeta1
[1] -0.002276
$Info$AtmosphericTransBeta2
[1] -0.00667
$Info$AtmosphericTransX
[1] 1.9
$Info$CameraTemperatureRangeMax
[1] 150
$Info$CameraTemperatureRangeMin
[1] -10
$Info$CameraTemperatureMaxClip
[1] 180
$Info$CameraTemperatureMinClip
[1] -40
$Info$CameraTemperatureMaxWarn
[1] 150
$Info$CameraTemperatureMinWarn
[1] -10
$Info$CameraTemperatureMaxSaturated
[1] 180
$Info$CameraTemperatureMinSaturated
[1] -60
$Info$CameraModel
[1] 3
$Info$CameraPartNumber
[1] "72003-0303"
$Info$CameraSerialNumber
[1] 720071224
$Info$CameraSoftware
[1] "34.0.0"
$Info$LensModel
[1] 2
$Info$LensPartNumber
[1] NA
$Info$LensSerialNumber
[1] NA
$Info$Isotherm1Color
[1] 100128128
$Info$Isotherm2Color
[1] 100110240
$Info$PaletteMethod
[1] 0
$Info$PaletteStretch
[1] 2
$Info$PaletteFileName
[1] "."
$Info$PaletteName
[1] ""
$Info$Palette
[1] "672-"
$Info$RawThermalImageWidth
[1] 80
$Info$RawThermalImageHeight
[1] 60
$Info$RawThermalImageType
[1] "PNG"
$Info$RawThermalImage
[1] "6352-"
$Info$Real2IR
[1] 1.389537
$Info$OffsetX
[1] -1
$Info$OffsetY
[1] 8
$Info$PiPX1
[1] 0
$Info$PiPX2
[1] 80
$Info$PiPY1
[1] 0
$Info$PiPY2
[1] 60
$Info$EmbeddedImageWidth
[1] 640
$Info$EmbeddedImageHeight
[1] 480
$Info$EmbeddedImageType
[1] ""
$Info$EmbeddedImage
[1] "38325-"
$Info$ImageWidth
[1] 320
$Info$ImageHeight
[1] 240
$Info$EncodingProcess
[1] ""
$Info$BitsPerSample
[1] 8
$Info$ColorComponents
[1] 3
$Info$YCbCrSubSampling
[1] "4:2:022"
$Info$ImageSize
[1] 320240
$Info$Megapixels
[1] 0.077
$Info$PeakSpectralSensitivity
[1] 10.9
$Info$ShutterSpeed
[1] 150
$Info$ThumbnailImage
[1] "2474-"
$Info$FocalLength
[1] 1.8
$Dates
$Dates$FileModificationDateTime
[1] "2017-08-21 14:20:46"
$Dates$FileAccessDateTime
[1] "2017-10-04 02:36:24"
$Dates$FileInodeChangeDateTime
[1] "2017-09-10 18:41:44"
$Dates$ModifyDate
[1] "2017-08-21 08:20:46"
$Dates$CreateDate
[1] "2017-08-21 08:20:46"
$Dates$DateTimeOriginal
[1] "2017-08-21 16:20:46"
Is there a way to get the measurement i.e. spot value from the image? Or can I use exiftool to extract this value from the image and how can I do that?
I would try using exiftool on the command line rather than from the R package, Thermimage, since the R package does not report on all text output from exiftool. Try this in a terminal window:
exiftool FLIR8655.jpg
which might yield something like this for your image:
...
Meas 1 Type : Spot
Meas 1 Params : 320 240
Meas 1 Label : 1
Meas 2 Type : Area
Meas 2 Params : 213 160 213 160
Meas 2 Label : 1
But I cannot find any output for the spot temperature (Meas 1 above) in the normal exiftool output. You could export the raw thermal binary data with exiftool and extract the wth x hth data point corresponding to the spot position.

Print and delete chr(0) entries in a list of strings

I have a vector of strings in R, some of its indices are chr(0). I want to print those indices which are chr(0) and then remove them. I am unable to do any of the two tasks. What I tried for first scenario are 3 different solutions:
(1) temp <- keepColumns[keepColumns == character(0)]
(2) temp2 <- which(keepColumns[]== "0")
(3) temp2 <- foreach(i=1:length(keepColumns)) %do% if (length(keepColumns[i]) == 0) print("Empty")
#if (identical(keepColumns[i],character(0))) { print(i) }
In all cases, value of temp turns out to be
> temp2
integer(0)
To remove chr(0), I have tried similar solutions:
keepColumnsList = keepColumns[!identical(keepColumns, character(0))]
but keepColumnsList is the same as the original vector.
The list is as follows:
> keepColumns
[[1]]
> keepColumns
[[1]]
[1] "P2.11FIC2026_PV"
[[2]]
[1] "P2.11FIC2046D_PV"
[[3]]
[1] "P2.11FI2046"
[[4]]
[1] "P2.11FY2048I"
[[5]]
[1] "P2.11FIC2030_PV"
[[6]]
[1] "P2.11FIC2011A_PV"
[[7]]
[1] "P2.11FIC2017_OP"
[[8]]
[1] "P2.11HIC5001"
[[9]]
[1] "P2.11HIC5002"
[[10]]
[1] "P2.11HIC5003"
[[11]]
[1] "P2.11PI5014"
[[12]]
[1] "P2.11TIC5003_PV"
[[13]]
[1] "P2.11TIC5011_PV"
[[14]]
[1] "P2.11FIC5011_PV"
[[15]]
character(0)
[[16]]
[1] "P2.11TI5001"
[[17]]
[1] "P2.11PIC2031_PV"
[[18]]
[1] "P2.11PIC2045_PV"
[[19]]
[1] "P2.11HIC2026E"
[[20]]
[1] "P2.11AI2001A1"
[[21]]
[1] "P2.11AI2001A7"
[[22]]
[1] "P2.11FI2029"
[[23]]
[1] "P2.11AI2026"
[[24]]
[1] "P2.11AI2001A8"
[[25]]
[1] "P2.11TI2076"
[[26]]
[1] "P2.11TI2068"
[[27]]
[1] "P2.11TI2027"
[[28]]
[1] "P2.11TI2071"
[[29]]
[1] "P2.11AI2001G6"
[[30]]
[1] "P2.11TI2047"
[[31]]
[1] "P2.11AI2001G1"
[[32]]
[1] "P2.11AI2001G2"
[[33]]
[1] "P2.11AI2001G3"
[[34]]
[1] "P2.11AI2001G4"
[[35]]
[1] "P2.11AI2001G5"
[[36]]
[1] "P2.11AI2001H5"
[[37]]
[1] "P2.11FI5001"
[[38]]
[1] "P2.11FI5021"
[[39]]
[1] "P2.11FI5023"
[[40]]
[1] "P2.11PI5004"
[[41]]
[1] "P2.11TI5009"
[[42]]
[1] "P2.11TI5010"
[[43]]
[1] "P2.11TI5026"
[[44]]
[1] "P2.11TI5034"
[[45]]
[1] "P2.11TI5036"
[[46]]
[1] "P2.11TI5038"
[[47]]
[1] "P2.11TI5045"
[[48]]
[1] "P2.11FI2001A"
[[49]]
[1] "P2.11FI2001B"
[[50]]
character(0)
[[51]]
character(0)
[[52]]
[1] "P2.11TI2061"
[[53]]
[1] "P2.11TI2062"
[[54]]
[1] "P2.11TI2063"
[[55]]
[1] "P2.11TI2064"
[[56]]
[1] "P2.11TI2065"
[[57]]
[1] "P2.11TI2066"
This works in removing the entries with chr(0) value:
Filter(function(x) length(x)==0, keepColumns)
To display of the indices of the list where chr(0) values are, the following should work:
Filter(function(x) length(keepColumns[[x]])==0, seq_along(keepColumns))
But still no solution for displaying index number of all chr(0) entries in a vector.

Calculate individual list totals and output as a vector

I have a list with 1000 elements, the elements of which are vectors of 100 values. I want to sum these elements but each time every list has the same value as an output. How can this be done?
[[1]]
[1] ....
...
[[1000]]
[1] 41.796588400 1.822177817 0.516105021 16.554318711 22.441116192 11.557223237
[7] 11.610201393 14.126722844 11.165417165 17.024791387 97.744736046 1.053429931
[13] 5.409970556 10.534262466 2.402112926 61.989253054 89.141315737 7.831002594
[19] 0.229311742 1.167366732 74.131595409 26.837412033 0.315262754 3.662595556
[25] 7.621307733 6.599907692 2.436551709 50.371429645 0.046652228 84.050028030
[31] 2.547629448 8.308966616 9.566100355 1.324906725 35.296845475 80.754003596
[37] 53.073032197 0.506524295 0.478822391 14.147898302 0.292336489 45.329947475
[43] 25.455486564 20.790057839 12.622231025 38.933121408 41.196719977 3.762513880
[49] 88.326438565 0.006009079 18.974940292 18.964924610 4.299943187 0.266114761
[55] 16.597228049 1.030058767 15.304970202 12.220887655 2.229263654 18.506392124
[61] 8.455070746 0.000839928 0.621677398 16.936509072 10.599982129 5.542332913
[67] 0.773795046 20.199178278 33.488631341 4.624800890 0.069347211 11.352912859
[73] 20.614961806 2.986133970 1.185518764 33.563723467 15.468933119 2.360548396
[79] 8.237662458 50.279689216 1.307944799 17.654806254 42.129699374 2.352254185
[85] 1.069597812 12.714936626 4.677094902 0.085737588 11.653287453 15.610804195
[91] 5.489030702 0.202041121 2.849800157 5.284956342 0.128010723 5.731836865
[97] 3.635845442 11.560654785 0.800697847 0.719558593
is it not as simple as:
lapply(x, sum)
? Here is what I get:
> x <- list(rep(1,100), rep(2,100), rep(3,100))
> lapply(x,length)
[[1]]
[1] 100
[[2]]
[1] 100
[[3]]
[1] 100
> lapply(x,head)
[[1]]
[1] 1 1 1 1 1 1
[[2]]
[1] 2 2 2 2 2 2
[[3]]
[1] 3 3 3 3 3 3
> lapply(x,sum)
[[1]]
[1] 100
[[2]]
[1] 200
[[3]]
[1] 300

combining lists into data.frame results in nested list instead of column

I am trying to concatenate two lists called source_names and communities into one list called names and then store it as a dataframe column, but I keep getting deeply nested lists instead. What did I do wrong?
My code:
> communities<-as.list(as.character(V(g)[(length(df[,1])+1):length(V(g))]))
> head(communities,5)
[[1]]
[1] "122"
[[2]]
[1] "123"
[[3]]
[1] "124"
[[4]]
[1] "125"
[[5]]
[1] "126"
> source_names<-as.list(df[,1])
> head(source_names,5)
[[1]]
[1] "11170"
[[2]]
[1] "2840"
[[3]]
[1] "32595"
[[4]]
[1] "45410"
[[5]]
[1] "52720"
> names<-c(source_names,communities)#force names to include communities
> names
[[1]]
[1] "11170"
[[2]]
[1] "2840"
[[3]]
[1] "32595"
[[4]]
[1] "45410"
[[5]]
[1] "52720"
[[6]]
[1] "61720"
[[7]]
[1] "7180"
[[8]]
[1] "81990"
[[9]]
[1] "93445"
[[10]]
[1] "102250"
[[11]]
[1] "111965"
[[12]]
[1] "123345"
[[13]]
[1] "134480"
[[14]]
[1] "142225"
[[15]]
[1] "153870"
[[16]]
[1] "162460"
[[17]]
[1] "171705"
[[18]]
[1] "182480"
[[19]]
[1] "193495"
[[20]]
[1] "201870"
[[21]]
[1] "214620"
[[22]]
[1] "22240"
[[23]]
[1] "231305"
[[24]]
[1] "240"
[[25]]
[1] "250"
[[26]]
[1] "266585"
[[27]]
[1] "276490"
[[28]]
[1] "281840"
[[29]]
[1] "293340"
[[30]]
[1] "305970"
[[31]]
[1] "313450"
[[32]]
[1] "323005"
[[33]]
[1] "332550"
[[34]]
[1] "34130"
[[35]]
[1] "350"
[[36]]
[1] "36580"
[[37]]
[1] "371395"
[[38]]
[1] "383595"
[[39]]
[1] "39455"
[[40]]
[1] "407385"
[[41]]
[1] "413170"
[[42]]
[1] "42580"
[[43]]
[1] "436890"
[[44]]
[1] "44295"
[[45]]
[1] "45730"
[[46]]
[1] "461005"
[[47]]
[1] "47605"
[[48]]
[1] "480"
[[49]]
[1] "495775"
[[50]]
[1] "501580"
[[51]]
[1] "510"
[[52]]
[1] "52645"
[[53]]
[1] "533510"
[[54]]
[1] "541620"
[[55]]
[1] "554870"
[[56]]
[1] "563855"
[[57]]
[1] "57210"
[[58]]
[1] "58700"
[[59]]
[1] "592460"
[[60]]
[1] "606840"
[[61]]
[1] "613490"
[[62]]
[1] "621910"
[[63]]
[1] "634475"
[[64]]
[1] "649665"
[[65]]
[1] "651060"
[[66]]
[1] "664735"
[[67]]
[1] "674325"
[[68]]
[1] "684580"
[[69]]
[1] "692950"
[[70]]
[1] "702805"
[[71]]
[1] "712435"
[[72]]
[1] "721080"
[[73]]
[1] "731330"
[[74]]
[1] "747575"
[[75]]
[1] "751675"
[[76]]
[1] "764560"
[[77]]
[1] "772385"
[[78]]
[1] "784215"
[[79]]
[1] "791705"
[[80]]
[1] "805420"
[[81]]
[1] "811950"
[[82]]
[1] "823120"
[[83]]
[1] "8310350"
[[84]]
[1] "844910"
[[85]]
[1] "85630"
[[86]]
[1] "86590"
[[87]]
[1] "875575"
[[88]]
[1] "888575"
[[89]]
[1] "891525"
[[90]]
[1] "905415"
[[91]]
[1] "914020"
[[92]]
[1] "92785"
[[93]]
[1] "930"
[[94]]
[1] "941615"
[[95]]
[1] "957770"
[[96]]
[1] "968065"
[[97]]
[1] "975225"
[[98]]
[1] "984015"
[[99]]
[1] "993880"
[[100]]
[1] "1002280"
[[101]]
[1] "101845"
[[102]]
[1] "102645"
[[103]]
[1] "1031220"
[[104]]
[1] "1042723"
[[105]]
[1] "1053670"
[[106]]
[1] "1060"
[[107]]
[1] "10780"
[[108]]
[1] "1080"
[[109]]
[1] "1091335"
[[110]]
[1] "1101805"
[[111]]
[1] "111270"
[[112]]
[1] "112480"
[[113]]
[1] "1131910"
[[114]]
[1] "1142585"
[[115]]
[1] "1150"
[[116]]
[1] "1160"
[[117]]
[1] "1171370"
[[118]]
[1] "1181725"
[[119]]
[1] "1190"
[[120]]
[1] "1200"
[[121]]
[1] "1210"
[[122]]
[1] "122"
[[123]]
[1] "123"
[[124]]
[1] "124"
[[125]]
[1] "125"
[[126]]
[1] "126"
[[127]]
[1] "127"
[[128]]
[1] "128"
[[129]]
[1] "129"
[[130]]
[1] "130"
[[131]]
[1] "131"
[[132]]
[1] "132"
[[133]]
[1] "133"
[[134]]
[1] "134"
[[135]]
[1] "135"
My expected output:
> names
11170
2840
32595
45410
52720
122
123
124
125
126
Just do unlist
names <- unlist(c(source_names, communities))
data.frame(names)
# names
#1 11170
#2 2840
#3 32595
#4 45410
#5 52720
#6 122
#7 123
#8 124
#9 125
#10 126

Numerical error in toy script to convert decimals into binary digits

I try to implement a toy script for converting decimals (0.21 was the input in the below example) into binary digits - everything is working fine, except for a numerical error that I don't know how to prevent:
bin_dec <- function() {
b <- as.numeric(readline("Input Binary digit: "))
dec=9999999
P=9999999
N=b
print("N B P U")
while (dec != b & P != 0) {
P = N*2
U = floor(P)
dec = P%%1
print(sprintf("%s 2 %s %s", N, P, U))
N = dec
}
}
> bin_dec()
Input Binary digit: 0.21
[1] "N B P U"
[1] "0.21 2 0.42 0"
[1] "0.42 2 0.84 0"
[1] "0.84 2 1.68 1"
[1] "0.68 2 1.36 1"
[1] "0.36 2 0.72 0"
[1] "0.72 2 1.44 1"
[1] "0.440000000000000 2 0.879999999999999 0"
[1] "0.879999999999999 2 1.76000000000000 1"
[1] "0.759999999999998 2 1.52000000000000 1"
[1] "0.519999999999996 2 1.03999999999999 1"
[1] "0.039999999999992 2 0.079999999999984 0"
[1] "0.079999999999984 2 0.159999999999968 0"
[1] "0.159999999999968 2 0.319999999999936 0"
[1] "0.319999999999936 2 0.639999999999873 0"
[1] "0.639999999999873 2 1.27999999999975 1"
[1] "0.279999999999745 2 0.559999999999491 0"
[1] "0.559999999999491 2 1.11999999999898 1"
[1] "0.119999999998981 2 0.239999999997963 0"
[1] "0.239999999997963 2 0.479999999995925 0"
[1] "0.479999999995925 2 0.95999999999185 0"
[1] "0.95999999999185 2 1.9199999999837 1"
[1] "0.919999999983702 2 1.83999999996740 1"
[1] "0.839999999967404 2 1.67999999993481 1"
[1] "0.679999999934807 2 1.35999999986961 1"
[1] "0.359999999869615 2 0.71999999973923 0"
[1] "0.71999999973923 2 1.43999999947846 1"
[1] "0.439999999478459 2 0.879999998956919 0"
[1] "0.879999998956919 2 1.75999999791384 1"
[1] "0.759999997913837 2 1.51999999582767 1"
[1] "0.519999995827675 2 1.03999999165535 1"
[1] "0.0399999916553497 2 0.0799999833106995 0"
[1] "0.0799999833106995 2 0.159999966621399 0"
[1] "0.159999966621399 2 0.319999933242798 0"
[1] "0.319999933242798 2 0.639999866485596 0"
[1] "0.639999866485596 2 1.27999973297119 1"
[1] "0.279999732971191 2 0.559999465942383 0"
[1] "0.559999465942383 2 1.11999893188477 1"
[1] "0.119998931884766 2 0.239997863769531 0"
[1] "0.239997863769531 2 0.479995727539062 0"
[1] "0.479995727539062 2 0.959991455078125 0"
[1] "0.959991455078125 2 1.91998291015625 1"
[1] "0.91998291015625 2 1.8399658203125 1"
[1] "0.8399658203125 2 1.679931640625 1"
[1] "0.679931640625 2 1.35986328125 1"
[1] "0.35986328125 2 0.7197265625 0"
[1] "0.7197265625 2 1.439453125 1"
[1] "0.439453125 2 0.87890625 0"
[1] "0.87890625 2 1.7578125 1"
[1] "0.7578125 2 1.515625 1"
[1] "0.515625 2 1.03125 1"
[1] "0.03125 2 0.0625 0"
[1] "0.0625 2 0.125 0"
[1] "0.125 2 0.25 0"
[1] "0.25 2 0.5 0"
[1] "0.5 2 1 1"
[1] "0 2 0 0"
> sessionInfo()
R version 2.7.2 (2008-08-25)
i386-pc-mingw32
Found it out myself... I'll paste the working code for the record.
However I've got one issue with cat() and print() which give me undesired NULLs at the end of a line... Strangely, cat without print alone breaks the loop.. And, print alone outputs strings, which I don't want.
bin_dec <- function() {
b <- as.numeric(readline("Input Binary digit: "))
dec=9999999
P=9999999
N=b
N_list=list()
bin=list()
cat("N B P U\n")
while (!dec%in%N_list[1:length(N_list)-1] & P != 0) {
P = N*2
U = floor(P)
dec = signif(P%%1)
print(cat(sprintf("%s 2 %s %s ", N, P, U)))
N = dec
N_list <- append(N_list, dec)
bin <- append(bin, U)
}
dig <- paste(rev(unlist(bin)), collapse = " ")
cat(paste("\nThe resulting binary digit is:\n\n***",
"0 . ", dig, "***\n\n", sep=""))
}
bin_dec()
Input Binary digit: 0.21
N B P U
0.21 2 0.42 0 NULL
0.42 2 0.84 0 NULL
0.84 2 1.68 1 NULL
0.68 2 1.36 1 NULL
0.36 2 0.72 0 NULL
0.72 2 1.44 1 NULL
0.44 2 0.88 0 NULL
0.88 2 1.76 1 NULL
0.76 2 1.52 1 NULL
0.52 2 1.04 1 NULL
0.04 2 0.08 0 NULL
0.08 2 0.16 0 NULL
0.16 2 0.32 0 NULL
0.32 2 0.64 0 NULL
0.64 2 1.28 1 NULL
0.28 2 0.56 0 NULL
0.56 2 1.12 1 NULL
0.12 2 0.24 0 NULL
0.24 2 0.48 0 NULL
0.48 2 0.96 0 NULL
0.96 2 1.92 1 NULL
0.92 2 1.84 1 NULL
The resulting binary digit is:
***0 . 1 1 0 0 0 1 0 1 0 0 0 0 1 1 1 0 1 0 1 1 0 0***

Resources