Scientific notation with Rmpfr in R - 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

Related

Get the mean for every iteration

I'm new in R. Hoping someone could help me.
I am trying to get the mean using for the first values of i for nth iteration, example (first value on first iteration then first two values on 2nd iterations)
How do I go about doing this?
Here is the sample data:
set.seed(1234)
i <- sample(200,100)
An alternative, may be, simpler solution
set.seed(1234)
i <- sample(200,100)
cumsum(i)/(1:100)
#> [1] 28.00000 54.00000 86.00000 89.75000 94.00000 101.16667 105.71429
#> [8] 113.25000 116.66667 118.20000 116.36364 115.25000 113.30769 110.21429
#> [15] 108.13333 108.62500 103.05882 104.33333 102.10526 97.20000 101.66667
#> [22] 103.81818 101.04348 100.70833 101.56000 105.11538 103.66667 105.96429
#> [29] 106.55172 104.60000 104.70968 105.53125 104.96970 103.08824 103.42857
#> [36] 102.55556 104.10811 102.47368 100.94872 98.47500 98.92683 101.00000
#> [43] 99.79070 99.84091 98.75556 99.52174 100.76596 101.87500 100.95918
#> [50] 101.66000 100.17647 101.03846 102.37736 100.62963 100.54545 99.14286
#> [57] 98.01754 99.20690 100.38983 100.15000 101.00000 99.53226 99.68254
#> [64] 100.34375 100.07692 101.39394 100.17910 99.75000 99.18841 99.85714
#> [71] 100.35211 100.72222 102.04110 101.02703 100.69333 101.53947 102.44156
#> [78] 101.89744 101.43038 100.61250 100.83951 102.04878 101.04819 99.95238
#> [85] 99.12941 98.70930 97.77011 98.44318 98.92135 98.46667 97.45055
#> [92] 97.31522 97.75269 97.05319 96.84211 97.02083 97.81443 97.93878
#> [99] 98.92929 99.55000
Created on 2022-03-04 by the reprex package (v2.0.1)
Here's a one-liner to get the result:
sapply(1:100, function(x) mean(i[seq(x)]))
#> [1] 28.00000 54.00000 86.00000 89.75000 94.00000 101.16667 105.71429
#> [8] 113.25000 116.66667 118.20000 116.36364 115.25000 113.30769 110.21429
#> [15] 108.13333 108.62500 103.05882 104.33333 102.10526 97.20000 101.66667
#> [22] 103.81818 101.04348 100.70833 101.56000 105.11538 103.66667 105.96429
#> [29] 106.55172 104.60000 104.70968 105.53125 104.96970 103.08824 103.42857
#> [36] 102.55556 104.10811 102.47368 100.94872 98.47500 98.92683 101.00000
#> [43] 99.79070 99.84091 98.75556 99.52174 100.76596 101.87500 100.95918
#> [50] 101.66000 100.17647 101.03846 102.37736 100.62963 100.54545 99.14286
#> [57] 98.01754 99.20690 100.38983 100.15000 101.00000 99.53226 99.68254
#> [64] 100.34375 100.07692 101.39394 100.17910 99.75000 99.18841 99.85714
#> [71] 100.35211 100.72222 102.04110 101.02703 100.69333 101.53947 102.44156
#> [78] 101.89744 101.43038 100.61250 100.83951 102.04878 101.04819 99.95238
#> [85] 99.12941 98.70930 97.77011 98.44318 98.92135 98.46667 97.45055
#> [92] 97.31522 97.75269 97.05319 96.84211 97.02083 97.81443 97.93878
#> [99] 98.92929 99.55000
Created on 2022-03-04 by the reprex package (v2.0.1)

Extract names of genes expressed by at least 10% of cells in a cluster

I have a Seurat object with defined clusters. I need to extract a list of all genes that are expressed by at least 10% of cells in my cluster. I need to repeat it for every cluster that I have, separately.
I know one code that could potentially extract genes expressed by at least 10% of cells from the whole Seurat:
genes.to.keep <- Matrix::rowSums(Monocyte.integrated#assays$RNA#counts > 0) >= floor(0.1 * ncol(Monocyte.integrated#assays$RNA#counts))
counts.sub <- Monocyte.integrated#assays$RNA#counts[genes.to.keep,]
But this is not what I want. And I'm not sure how to modify it to include cluster names (considering it's correct).
I store the cluster names in the metadata variable called "cluster_names".
I would appreciate any help
BW
You could use lapply to iterate over the factor levels of your clusters to subset and filter them individually and use setNames to name the resulting list. Below is a reproducible example:
library(Seurat)
data("pbmc_small")
pbmc_small <- FindClusters(pbmc_small, resolution = 1)
names(pbmc_small#meta.data)[names(pbmc_small#meta.data)=="seurat_clusters"] <- "cluster_names"
levels(pbmc_small$cluster_names) <- paste0("cluster_", seq_along(levels(pbmc_small$cluster_names)))
setNames(lapply(levels(pbmc_small$cluster_names), function(x) {
p <- subset(pbmc_small, cluster_names==x)
rownames(p)[Matrix::rowSums(p#assays$RNA#counts > 0) >= .1*dim(p)[2]]
}), levels(pbmc_small$cluster_names))
#> $cluster_1
#> [1] "CD79B" "HLA-DRA" "LTB" "SP100" "PPP3CC" "CXCR4"
#> [7] "STX10" "SNHG7" "CD3D" "NOSIP" "SAFB2" "CD2"
#> [13] "IL7R" "PIK3IP1" "MPHOSPH6" "KHDRBS1" "MAL" "CCR7"
#> [19] "THYN1" "TAF7" "LDHB" "TMEM123" "EPC1" "EIF4A2"
#> [25] "CD3E" "TMUB1" "BLOC1S4" "SRSF7" "ACAP1" "TNFAIP8"
#> [31] "CD7" "TAGAP" "DNAJB1" "ASNSD1" "S1PR4" "CTSW"
#> [37] "GZMK" "NKG7" "IL32" "DNAJC2" "LYAR" "CST7"
#> [43] "LCK" "CCL5" "HNRNPH1" "SSR2" "GIMAP1" "MMADHC"
#> [49] "CD8A" "GYPC" "HNRNPF" "RPL7L1" "KLRG1" "CRBN"
#> [55] "SATB1" "PMPCB" "NRBP1" "TCF7" "HNRNPA3" "S100A8"
#> [61] "S100A9" "LYZ" "FCN1" "TYROBP" "NFKBIA" "TYMP"
#> [67] "CTSS" "TSPO" "CTSB" "LGALS1" "BLVRA" "LGALS3"
#> [73] "IFI6" "HLA-DPA1" "CST3" "GSTP1" "EIF3G" "VPS28"
#> [79] "ZFP36L1" "ANXA2" "HSP90AA1" "LST1" "AIF1" "PSAP"
#> [85] "YWHAB" "MYO1G" "SAT1" "RGS2" "FCGR3A" "S100A11"
#> [91] "FCER1G" "IFITM2" "COTL1" "LGALS9" "CD68" "RHOC"
#> [97] "CARD16" "COPS6" "PPBP" "GPX1" "TPM4" "PF4"
#> [103] "SDPR" "NRGN" "SPARC" "GNG11" "CLU" "HIST1H2AC"
#> [109] "NCOA4" "GP9" "FERMT3" "ODC1" "CD9" "RUFY1"
#> [115] "TUBB1" "TALDO1" "TREML1" "NGFRAP1" "PGRMC1" "CA2"
#> [121] "ITGA2B" "MYL9" "TMEM40" "PARVB" "PTCRA" "ACRBP"
#> [127] "TSC22D1" "VDAC3" "GZMB" "GZMA" "GNLY" "FGFBP2"
#> [133] "AKR1C3" "CCL4" "PRF1" "GZMH" "XBP1" "GZMM"
#> [139] "PTGDR" "IGFBP7" "TTC38" "KLRD1" "ARHGDIA" "IL2RB"
#> [145] "CLIC3" "PPP1R18" "CD247" "ALOX5AP" "XCL2" "C12orf75"
#> [151] "RARRES3" "PCMT1" "LAMP1" "SPON2"
#>
#> $cluster_2
#> [1] "CD79B" "CD79A" "HLA-DRA" "HLA-DQB1"
#> [5] "HVCN1" "HLA-DMB" "LTB" "SP100"
#> [9] "NCF1" "EAF2" "FAM96A" "CXCR4"
#> [13] "STX10" "SNHG7" "NT5C" "NOSIP"
#> [17] "IL7R" "KHDRBS1" "TAF7" "LDHB"
#> [21] "TMEM123" "EIF4A2" "TMUB1" "BLOC1S4"
#> [25] "SRSF7" "TNFAIP8" "TAGAP" "DNAJB1"
#> [29] "S1PR4" "NKG7" "IL32" "DNAJC2"
#> [33] "LYAR" "CCL5" "SSR2" "GIMAP1"
#> [37] "MMADHC" "HNRNPF" "RPL7L1" "HNRNPA3"
#> [41] "S100A8" "S100A9" "LYZ" "CD14"
#> [45] "FCN1" "TYROBP" "ASGR1" "NFKBIA"
#> [49] "TYMP" "CTSS" "TSPO" "RBP7"
#> [53] "CTSB" "LGALS1" "FPR1" "VSTM1"
#> [57] "BLVRA" "MPEG1" "BID" "SMCO4"
#> [61] "CFD" "LINC00936" "LGALS2" "MS4A6A"
#> [65] "FCGRT" "LGALS3" "NUP214" "SCO2"
#> [69] "IL17RA" "IFI6" "HLA-DPA1" "FCER1A"
#> [73] "CLEC10A" "HLA-DMA" "RGS1" "HLA-DPB1"
#> [77] "HLA-DQA1" "RNF130" "HLA-DRB5" "HLA-DRB1"
#> [81] "CST3" "IL1B" "POP7" "HLA-DQA2"
#> [85] "GSTP1" "EIF3G" "VPS28" "LY86"
#> [89] "ZFP36L1" "ANXA2" "GRN" "CFP"
#> [93] "HSP90AA1" "LST1" "AIF1" "PSAP"
#> [97] "YWHAB" "MYO1G" "SAT1" "RGS2"
#> [101] "SERPINA1" "IFITM3" "FCGR3A" "LILRA3"
#> [105] "S100A11" "FCER1G" "TNFRSF1B" "IFITM2"
#> [109] "WARS" "IFI30" "MS4A7" "C5AR1"
#> [113] "HCK" "COTL1" "LGALS9" "CD68"
#> [117] "RP11-290F20.3" "RHOC" "CARD16" "LRRC25"
#> [121] "COPS6" "ADAR" "GPX1" "TPM4"
#> [125] "NRGN" "NCOA4" "FERMT3" "ODC1"
#> [129] "TALDO1" "PARVB" "VDAC3" "GZMB"
#> [133] "XBP1" "IGFBP7" "ARHGDIA" "PPP1R18"
#> [137] "ALOX5AP" "RARRES3" "PCMT1" "SPON2"
#>
#> $cluster_3
#> [1] "MS4A1" "CD79B" "CD79A" "HLA-DRA"
#> [5] "TCL1A" "HLA-DQB1" "HVCN1" "HLA-DMB"
#> [9] "LTB" "LINC00926" "FCER2" "SP100"
#> [13] "NCF1" "PPP3CC" "EAF2" "PPAPDC1B"
#> [17] "CD19" "KIAA0125" "CYB561A3" "CD180"
#> [21] "RP11-693J15.5" "FAM96A" "CXCR4" "STX10"
#> [25] "SNHG7" "NT5C" "BANK1" "IGLL5"
#> [29] "CD200" "FCRLA" "CD3D" "NOSIP"
#> [33] "CD2" "IL7R" "PIK3IP1" "KHDRBS1"
#> [37] "THYN1" "TAF7" "LDHB" "TMEM123"
#> [41] "CCDC104" "EPC1" "EIF4A2" "CD3E"
#> [45] "SRSF7" "ACAP1" "TNFAIP8" "CD7"
#> [49] "TAGAP" "DNAJB1" "S1PR4" "CTSW"
#> [53] "GZMK" "NKG7" "IL32" "DNAJC2"
#> [57] "LYAR" "CST7" "LCK" "CCL5"
#> [61] "HNRNPH1" "SSR2" "GIMAP1" "MMADHC"
#> [65] "CD8A" "PTPN22" "GYPC" "HNRNPF"
#> [69] "RPL7L1" "CRBN" "SATB1" "SIT1"
#> [73] "PMPCB" "NRBP1" "TCF7" "HNRNPA3"
#> [77] "S100A9" "LYZ" "FCN1" "TYROBP"
#> [81] "NFKBIA" "TYMP" "CTSS" "TSPO"
#> [85] "CTSB" "LGALS1" "BLVRA" "MPEG1"
#> [89] "BID" "CFD" "LINC00936" "LGALS2"
#> [93] "MS4A6A" "FCGRT" "LGALS3" "SCO2"
#> [97] "HLA-DPA1" "FCER1A" "CLEC10A" "HLA-DMA"
#> [101] "RGS1" "HLA-DPB1" "HLA-DQA1" "RNF130"
#> [105] "HLA-DRB5" "HLA-DRB1" "CST3" "IL1B"
#> [109] "POP7" "HLA-DQA2" "CD1C" "GSTP1"
#> [113] "EIF3G" "VPS28" "LY86" "ZFP36L1"
#> [117] "ZNF330" "ANXA2" "GRN" "CFP"
#> [121] "HSP90AA1" "FUOM" "LST1" "AIF1"
#> [125] "PSAP" "YWHAB" "MYO1G" "SAT1"
#> [129] "RGS2" "SERPINA1" "IFITM3" "FCGR3A"
#> [133] "S100A11" "FCER1G" "TNFRSF1B" "IFITM2"
#> [137] "WARS" "IFI30" "MS4A7" "HCK"
#> [141] "COTL1" "LGALS9" "CD68" "RHOC"
#> [145] "CARD16" "LRRC25" "COPS6" "ADAR"
#> [149] "GPX1" "TPM4" "NCOA4" "FERMT3"
#> [153] "ODC1" "RUFY1" "TALDO1" "VDAC3"
#> [157] "GZMA" "GNLY" "FGFBP2" "PRF1"
#> [161] "XBP1" "GZMM" "PTGDR" "ARHGDIA"
#> [165] "PPP1R18" "CD247" "ALOX5AP" "XCL2"
#> [169] "C12orf75" "RARRES3" "PCMT1" "SPON2"
Created on 2021-03-26 by the reprex package (v1.0.0)

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.

assign to is.na(clinical.trial$age)

I am looking at the code from here which has this at the beginning:
## generate data for medical example
clinical.trial <-
data.frame(patient = 1:100,
age = rnorm(100, mean = 60, sd = 6),
treatment = gl(2, 50,
labels = c("Treatment", "Control")),
center = sample(paste("Center", LETTERS[1:5]), 100, replace =
TRUE))
## set some ages to NA (missing)
is.na(clinical.trial$age) <- sample(1:100, 20)
I cannot understand this last line.
The LHS is a vector of all FALSE values. The RHS is a vector of 20 numbers selected from the vector 1:100.
I don't understand this kind of assignment. How is this result in clinical.trial$age getting some NA values? Does this kind of assignment have a name? At best I would say that the boolean vector on the RHS gets numbers assigned to it with recycling.
is.na(x) <- value is translated as 'is.na<-'(x, value).
You can think of 'is.na<-'(x, value) as 'assign NA to x, at position value'.
A perhaps better and intuitive phrasing could be assign_NA(to = x, pos = value).
Regarding other similar function, we can find those in the base package:
x <- as.character(lsf.str("package:base"))
x[grep('<-', x)]
#> [1] "$<-" "$<-.data.frame"
#> [3] "#<-" "[[<-"
#> [5] "[[<-.data.frame" "[[<-.factor"
#> [7] "[[<-.numeric_version" "[<-"
#> [9] "[<-.data.frame" "[<-.Date"
#> [11] "[<-.factor" "[<-.numeric_version"
#> [13] "[<-.POSIXct" "[<-.POSIXlt"
#> [15] "<-" "<<-"
#> [17] "attr<-" "attributes<-"
#> [19] "body<-" "class<-"
#> [21] "colnames<-" "comment<-"
#> [23] "diag<-" "dim<-"
#> [25] "dimnames<-" "dimnames<-.data.frame"
#> [27] "Encoding<-" "environment<-"
#> [29] "formals<-" "is.na<-"
#> [31] "is.na<-.default" "is.na<-.factor"
#> [33] "is.na<-.numeric_version" "length<-"
#> [35] "length<-.factor" "levels<-"
#> [37] "levels<-.factor" "mode<-"
#> [39] "mostattributes<-" "names<-"
#> [41] "names<-.POSIXlt" "oldClass<-"
#> [43] "parent.env<-" "regmatches<-"
#> [45] "row.names<-" "row.names<-.data.frame"
#> [47] "row.names<-.default" "rownames<-"
#> [49] "split<-" "split<-.data.frame"
#> [51] "split<-.default" "storage.mode<-"
#> [53] "substr<-" "substring<-"
#> [55] "units<-" "units<-.difftime"
All works the same in the sense that 'fun<-'(x, val) is equivalent to fun(x) <- val. But after that they all behave like any normal functions.
R manuals: 3.4.4 Subset assignment
The help tells us, that:
(xx <- c(0:4))
is.na(xx) <- c(2, 4)
xx #> 0 NA 2 NA 4
So,
is.na(xx) <- 1
behaves more like
set NA at position 1 on variable xx
#matt, to respond to your question asked above in the comments, here's an alternative way to do the same assignment that I think is easier to follow :-)
clinical.trial$age[sample(1:100, 20)] <- NA

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

Resources