Calculate individual list totals and output as a vector - r

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

Related

How to get from list with indication in numbers to data frame in R

How to I convert the following list to a data frame? I have tried using data.frame but I get a data frame with 1 object and all the information in the rows of that object.
Is there a way to capture the numbers so I get the correct data per row (two rows, one per page) and with the correct labels (second list below)?
[[1]]
[[1]][[1]]
[1] "https://page1"
[[1]][[2]]
[1] 4534
[[1]][[3]]
[1] 3453
[[1]][[4]]
[1] 2343
[[1]][[5]]
[1] 0.2806075
[[1]][[6]]
[1] 0.4386998
[[2]]
[[2]][[1]]
[1] "https://page2"
[[2]][[2]]
[1] 9166
[[2]][[3]]
[1] 6294
[[2]][[4]]
[1] 698
[[2]][[5]]
[1] 0.1489971
[[2]][[6]]
[1] 0.1963775
list number two:
$columns
$columns[[1]]
[1] "event_url"
$columns[[2]]
[1] "page_views"
$columns[[3]]
[1] "unique_page_views"
$columns[[4]]
[1] "entries"
$columns[[5]]
[1] "bounce_rate_events"
$columns[[6]]
[1] "exit_rate"
You could do:
do.call(rbind, lapply(list1, function(x) setNames(as.data.frame(x), unlist(list2))))
#> event_url page_views unique_page_views entries bounce_rate_events exit_rate
#> 1 https://page1 4534 3453 2343 0.2806075 0.4386998
#> 2 https://page2 9166 6294 698 0.1489971 0.1963775
Data
list1 <- list(
list("https://page1", 4534, 3453, 2343, 0.2806075, 0.4386998),
list("https://page2", 9166, 6294, 698, 0.1489971, 0.1963775))
list2 <- list(columns = list("event_url", "page_views",
"unique_page_views", "entries",
"bounce_rate_events", "exit_rate"))
Using tidyverse
library(dplyr)
library(purrr)
map_dfr(list1, bind_cols) %>%
set_names(unlist(list2))
-output
# A tibble: 2 x 6
# event_url page_views unique_page_views entries bounce_rate_events exit_rate
# <chr> <dbl> <dbl> <dbl> <dbl> <dbl>
#1 https://page1 4534 3453 2343 0.281 0.439
#2 https://page2 9166 6294 698 0.149 0.196

Scientific notation with Rmpfr in R

I am testing some calculation based on the example code from Rmpfr in R.
The test is as follows:
ns <- mpfr(1:24, 120) ; factorial(ns)
However, in my output the result is:
[1] 1e0 2e0 6e0
[4] 2.4e1 1.2e2 7.2e2
[7] 5.04e3 4.032e4 3.6288e5
[10] 3.6288e6 3.99168e7 4.790016e8
[13] 6.2270208e9 8.71782912e10 1.307674368e12
[16] 2.0922789888e13 3.55687428096e14 6.402373705728e15
[19] 1.21645100408832e17 2.43290200817664e18 5.109094217170944e19
[22] 1.12400072777760768e21 2.585201673888497664e22 6.2044840173323943936e23
While in the example output, the scientific notation is got rid of as :
[1] 1 2
[3] 6 24
[5] 120 720
[7] 5040 40320
[9] 362880 3628800
[11] 39916800 479001600
[13] 6227020800 87178291200
[15] 1307674368000 20922789888000
[17] 355687428096000 6402373705728000
[19] 121645100408832000 2432902008176640000
[21] 51090942171709440000 1124000727777607680000
[23] 25852016738884976640000 620448401733239439360000
How could I turn off the scientific notation in this case?
I have tried:
options(scipen=999)
mpfr(1:24, 120,scientific=FALSE)
But none of them are working.
use scipen=0
library(Rmpfr)
options(scipen = 999)
ns <- mpfr(1:24, 120) ; factorial(ns)
# 24 'mpfr' numbers of precision 120 bits
# [1] 1e0 2e0 6e0
# [4] 2.e1 1.e2 7.e2
# [7] 5.0e3 4.03e4 3.628e5
# [10] 3.628e6 3.9916e7 4.79001e8
# [13] 6.227020e9 8.7178291e10 1.30767436e12
# [16] 2.092278988e13 3.5568742809e14 6.40237370572e15
# [19] 1.2164510040883e17 2.4329020081766e18 5.10909421717094e19
# [22] 1.1240007277776076e21 2.58520167388849766e22 6.204484017332394393e23
options(scipen = 0)
ns <- mpfr(1:24, 120) ; factorial(ns)
# 24 'mpfr' numbers of precision 120 bits
# [1] 1 2 6
# [4] 24 120 720
# [7] 5040 40320 362880
# [10] 3628800 39916800 479001600
# [13] 6227020800 87178291200 1307674368000
# [16] 20922789888000 355687428096000 6402373705728000
# [19] 121645100408832000 2432902008176640000 51090942171709440000
# [22] 1124000727777607680000 25852016738884976640000 620448401733239439360000

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.

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

Resources