I am working with temperature (Kelvin) and incident data and have created new columns with Celsius conversion and I would like to round the celsius values to the nearest whole number and also group in groups of 4 numbers. Such as 29.15 celsius is rounded to 29 and grouped in bins(?) of every 4 numbers. The groups would start at zero and contain 4 digits, e.g. 0-3, 4-7, 8-11, 12-15 etc. Sorry I am trying to think of better words to use but I am quite new to R. How would I round and group this way? Below is the code I have used so far and the result, but it isn't rounding or grouping as I need. Thanks so much!
tempDF <- data.frame(Kelvin = seq(240,320)) %>% #define an empty dat frame with temperatures going from 240 - 320 Kelvin
mutate(Celsius = Kelvin - 273.15) %>%
merge(New_AllTime_Temp, by.x = "Kelvin", by.y = "Temp", all.x = TRUE) %>% #Merge New_AllTime_Temp into the empty data frame, mapping each temperature to the data frame
merge(New_Incident_Temp, by.x = "Kelvin", by.y = "temp", all.x = TRUE) %>% #Merge New_Incident_Temp into the empty datframe, keeping temperature mapping
replace(is.na(.), 0) %>% ## Replace NA values with zeroes
mutate(norm_counnt = scales::rescale(counnt, to=c(0,1))) %>%
mutate(norm_incident = scales::rescale(incidents, to=c(0,1))) %>%
mutate(diffs = norm_incident - norm_counnt) %>%
mutate(rounded = round(Celsius, -2:4))```
"Kelvin" "Celsius" "counnt" "incidents" "norm_counnt" "norm_incident" "diffs" "rounded"
"1" 240 -33.15 0 0 0 0 0 0
"2" 241 -32.15 0 0 0 0 0 -30
"3" 242 -31.15 0 0 0 0 0 -31
"4" 243 -30.15 3 0 0.00146056475170399 0 -0.00146056475170399 -30.1
"5" 244 -29.15 9 0 0.00438169425511198 0 -0.00438169425511198 -29.15
"6" 245 -28.15 7 0 0.00340798442064265 0 -0.00340798442064265 -28.15
"7" 246 -27.15 11 1 0.0053554040895813 0.0196078431372549 0.0142524390476736 -27.15
"8" 247 -26.15 15 0 0.00730282375851996 0 -0.00730282375851996 0
"9" 248 -25.15 22 1 0.0107108081791626 0.0196078431372549 0.00889703495809229 -30
"10" 249 -24.15 11 1 0.0053554040895813 0.0196078431372549 0.0142524390476736 -24
"11" 250 -23.15 32 0 0.0155793573515093 0 -0.0155793573515093 -23.1
"12" 251 -22.15 33 0 0.0160662122687439 0 -0.0160662122687439 -22.15
"13" 252 -21.15 47 0 0.0228821811100292 0 -0.0228821811100292 -21.15
"14" 253 -20.15 107 1 0.0520934761441091 0.0196078431372549 -0.0324856330068542 -20.15
"15" 254 -19.15 117 0 0.0569620253164557 0 -0.0569620253164557 0
"16" 255 -18.15 162 2 0.0788704965920156 0.0392156862745098 -0.0396548103175058 -20
"17" 256 -17.15 221 4 0.107594936708861 0.0784313725490196 -0.0291635641598412 -17
"18" 257 -16.15 258 2 0.125608568646543 0.0392156862745098 -0.0863928823720335 -16.1
"19" 258 -15.15 272 3 0.132424537487829 0.0588235294117647 -0.0736010080760639 -15.15
"20" 259 -14.15 314 4 0.152872444011685 0.0784313725490196 -0.0744410714626649 -14.15
"21" 260 -13.15 409 4 0.199123661148978 0.0784313725490196 -0.120692288599958 -13.15
"22" 261 -12.15 478 11 0.232716650438169 0.215686274509804 -0.0170303759283655 0
"23" 262 -11.15 523 13 0.254625121713729 0.254901960784314 0.0002768390705844 -10
"24" 263 -10.15 574 8 0.279454722492697 0.156862745098039 -0.122591977394658 -10
"25" 264 -9.14999999999998 793 9 0.386075949367089 0.176470588235294 -0.209605361131794 -9.1
"26" 265 -8.14999999999998 924 14 0.44985394352483 0.274509803921569 -0.175344139603261 -8.15
"27" 266 -7.14999999999998 1108 18 0.539435248296008 0.352941176470588 -0.186494071825419 -7.15
"28" 267 -6.14999999999998 1082 17 0.526777020447907 0.333333333333333 -0.193443687114573 -6.15
"29" 268 -5.14999999999998 1198 15 0.583252190847128 0.294117647058824 -0.289134543788304 0
"30" 269 -4.14999999999998 1233 13 0.600292112950341 0.254901960784314 -0.345390152166027 0
"31" 270 -3.14999999999998 1244 17 0.605647517039922 0.333333333333333 -0.272314183706589 -3
"32" 271 -2.14999999999998 1496 32 0.728334956183057 0.627450980392157 -0.100883975790901 -2.1
"33" 272 -1.14999999999998 1565 25 0.761927945472249 0.490196078431373 -0.271731867040877 -1.15
"34" 273 -0.149999999999977 1870 35 0.910418695228822 0.686274509803922 -0.2241441854249 -0.15
"35" 274 0.850000000000023 2054 31 1 0.607843137254902 -0.392156862745098 0.85
"36" 275 1.85000000000002 2034 29 0.990262901655307 0.568627450980392 -0.421635450674915 0
"37" 276 2.85000000000002 1974 33 0.961051606621227 0.647058823529412 -0.313992783091815 0
"38" 277 3.85000000000002 1966 32 0.95715676728335 0.627450980392157 -0.329705786891193 4
"39" 278 4.85000000000002 2040 51 0.993184031158715 1 0.00681596884128532 4.9
"40" 279 5.85000000000002 1949 29 0.94888023369036 0.568627450980392 -0.380252782709968 5.85
"41" 280 6.85000000000002 2053 40 0.999513145082765 0.784313725490196 -0.215199419592569 6.85
"42" 281 7.85000000000002 1987 34 0.967380720545277 0.666666666666667 -0.300714053878611 7.85
"43" 282 8.85000000000002 1959 40 0.953748782862707 0.784313725490196 -0.169435057372511 0
"44" 283 9.85000000000002 1770 32 0.861733203505355 0.627450980392157 -0.234282223113199 10
"45" 284 10.85 1816 27 0.88412852969815 0.529411764705882 -0.354716764992268 11
"46" 285 11.85 1859 39 0.905063291139241 0.764705882352941 -0.140357408786299 11.9
"47" 286 12.85 2029 35 0.987828627069133 0.686274509803922 -0.301554117265212 12.85
"48" 287 13.85 1926 33 0.937682570593963 0.647058823529412 -0.290623747064551 13.85
"49" 288 14.85 1848 43 0.899707887049659 0.843137254901961 -0.0565706321476984 14.85
"50" 289 15.85 1823 33 0.887536514118793 0.647058823529412 -0.240477690589381 0
"51" 290 16.85 1662 24 0.809152872444012 0.470588235294118 -0.338564637149894 20
"52" 291 17.85 1578 31 0.7682570593963 0.607843137254902 -0.160413922141398 18
"53" 292 18.85 1425 12 0.693768257059396 0.235294117647059 -0.458474139412337 18.9
"54" 293 19.85 1318 17 0.641674780915287 0.333333333333333 -0.308341447581954 19.85
"55" 294 20.85 1204 19 0.586173320350535 0.372549019607843 -0.213624300742692 20.85
"56" 295 21.85 1029 18 0.500973709834469 0.352941176470588 -0.148032533363881 21.85
"57" 296 22.85 876 12 0.426484907497566 0.235294117647059 -0.191190789850507 0
"58" 297 23.85 735 13 0.357838364167478 0.254901960784314 -0.102936403383164 20
"59" 298 24.85 623 5 0.303310613437196 0.0980392156862745 -0.205271397750921 25
"60" 299 25.85 571 7 0.277994157740993 0.137254901960784 -0.140739255780209 25.9
"61" 300 26.85 512 5 0.249269717624148 0.0980392156862745 -0.151230501937874 26.85
"62" 301 27.85 417 5 0.203018500486855 0.0980392156862745 -0.10497928480058 27.85
"63" 302 28.85 345 14 0.167964946445959 0.274509803921569 0.10654485747561 28.85
"64" 303 29.85 294 6 0.143135345666991 0.117647058823529 -0.0254882868434618 0
"65" 304 30.85 253 3 0.12317429406037 0.0588235294117647 -0.0643507646486053 30
"66" 305 31.85 198 3 0.0963972736124635 0.0588235294117647 -0.0375737442006988 32
"67" 306 32.85 128 2 0.062317429406037 0.0392156862745098 -0.0231017431315272 32.9
"68" 307 33.85 88 2 0.0428432327166504 0.0392156862745098 -0.00362754644214063 33.85
"69" 308 34.85 64 1 0.0311587147030185 0.0196078431372549 -0.0115508715657636 34.85
"70" 309 35.85 48 0 0.0233690360272639 0 -0.0233690360272639 35.85
"71" 310 36.85 20 0 0.00973709834469328 0 -0.00973709834469328 0
"72" 311 37.85 16 0 0.00778967867575463 0 -0.00778967867575463 40
"73" 312 38.85 7 0 0.00340798442064265 0 -0.00340798442064265 39
"74" 313 39.85 1 0 0.000486854917234664 0 -0.000486854917234664 39.9
"75" 314 40.85 0 0 0 0 0 40.85
"76" 315 41.85 0 0 0 0 0 41.85
"77" 316 42.85 0 0 0 0 0 42.85
"78" 317 43.85 0 0 0 0 0 0
"79" 318 44.85 0 0 0 0 0 40
"80" 319 45.85 0 0 0 0 0 46
"81" 320 46.85 0 0 0 0 0 46.9
Rounding can be done via the aptly named round function.
The cut function is made for continuous data, so instead of a group ranging from 0 to 3 and a different one from 4 to 7 we can just cut the continuum of real numbers at -.5, 3.5, 7.5, 11.5, ...
library(magrittr)
unrounded <- c(-12.6, -12.4, -.01, +.01, 12.4, 12.6)
rounded <- unrounded %>% round(digits = 0)
values <- c(1, 2, 4, 7, 10 ,20)
group <- values %>% cut(breaks = seq(-.5, 1000, 4))
It wasn't clear to me what you want to do with values less than zero but here's a tidyverse solution...
library(dplyr)
tempDF <- data.frame(Kelvin = seq(240,320)) %>%
mutate(Celsius = Kelvin - 273.15) %>%
mutate(Celsius_rounded = round(Celsius)) %>%
mutate(Celsius_groups = cut(Celsius_rounded, breaks = seq(-.5, 1000, 4)))
tempDF
#> Kelvin Celsius Celsius_rounded Celsius_groups
#> 1 240 -33.15 -33 <NA>
#> 2 241 -32.15 -32 <NA>
#> 3 242 -31.15 -31 <NA>
#> 4 243 -30.15 -30 <NA>
#> 5 244 -29.15 -29 <NA>
#> 6 245 -28.15 -28 <NA>
#> 7 246 -27.15 -27 <NA>
#> 8 247 -26.15 -26 <NA>
#> 9 248 -25.15 -25 <NA>
#> 10 249 -24.15 -24 <NA>
#> 11 250 -23.15 -23 <NA>
#> 12 251 -22.15 -22 <NA>
#> 13 252 -21.15 -21 <NA>
#> 14 253 -20.15 -20 <NA>
#> 15 254 -19.15 -19 <NA>
#> 16 255 -18.15 -18 <NA>
#> 17 256 -17.15 -17 <NA>
#> 18 257 -16.15 -16 <NA>
#> 19 258 -15.15 -15 <NA>
#> 20 259 -14.15 -14 <NA>
#> 21 260 -13.15 -13 <NA>
#> 22 261 -12.15 -12 <NA>
#> 23 262 -11.15 -11 <NA>
#> 24 263 -10.15 -10 <NA>
#> 25 264 -9.15 -9 <NA>
#> 26 265 -8.15 -8 <NA>
#> 27 266 -7.15 -7 <NA>
#> 28 267 -6.15 -6 <NA>
#> 29 268 -5.15 -5 <NA>
#> 30 269 -4.15 -4 <NA>
#> 31 270 -3.15 -3 <NA>
#> 32 271 -2.15 -2 <NA>
#> 33 272 -1.15 -1 <NA>
#> 34 273 -0.15 0 (-0.5,3.5]
#> 35 274 0.85 1 (-0.5,3.5]
#> 36 275 1.85 2 (-0.5,3.5]
#> 37 276 2.85 3 (-0.5,3.5]
#> 38 277 3.85 4 (3.5,7.5]
#> 39 278 4.85 5 (3.5,7.5]
#> 40 279 5.85 6 (3.5,7.5]
#> 41 280 6.85 7 (3.5,7.5]
#> 42 281 7.85 8 (7.5,11.5]
#> 43 282 8.85 9 (7.5,11.5]
#> 44 283 9.85 10 (7.5,11.5]
#> 45 284 10.85 11 (7.5,11.5]
#> 46 285 11.85 12 (11.5,15.5]
#> 47 286 12.85 13 (11.5,15.5]
#> 48 287 13.85 14 (11.5,15.5]
#> 49 288 14.85 15 (11.5,15.5]
#> 50 289 15.85 16 (15.5,19.5]
#> 51 290 16.85 17 (15.5,19.5]
#> 52 291 17.85 18 (15.5,19.5]
#> 53 292 18.85 19 (15.5,19.5]
#> 54 293 19.85 20 (19.5,23.5]
#> 55 294 20.85 21 (19.5,23.5]
#> 56 295 21.85 22 (19.5,23.5]
#> 57 296 22.85 23 (19.5,23.5]
#> 58 297 23.85 24 (23.5,27.5]
#> 59 298 24.85 25 (23.5,27.5]
#> 60 299 25.85 26 (23.5,27.5]
#> 61 300 26.85 27 (23.5,27.5]
#> 62 301 27.85 28 (27.5,31.5]
#> 63 302 28.85 29 (27.5,31.5]
#> 64 303 29.85 30 (27.5,31.5]
#> 65 304 30.85 31 (27.5,31.5]
#> 66 305 31.85 32 (31.5,35.5]
#> 67 306 32.85 33 (31.5,35.5]
#> 68 307 33.85 34 (31.5,35.5]
#> 69 308 34.85 35 (31.5,35.5]
#> 70 309 35.85 36 (35.5,39.5]
#> 71 310 36.85 37 (35.5,39.5]
#> 72 311 37.85 38 (35.5,39.5]
#> 73 312 38.85 39 (35.5,39.5]
#> 74 313 39.85 40 (39.5,43.5]
#> 75 314 40.85 41 (39.5,43.5]
#> 76 315 41.85 42 (39.5,43.5]
#> 77 316 42.85 43 (39.5,43.5]
#> 78 317 43.85 44 (43.5,47.5]
#> 79 318 44.85 45 (43.5,47.5]
#> 80 319 45.85 46 (43.5,47.5]
#> 81 320 46.85 47 (43.5,47.5]
Related
I'm trying to use rBlast for protein sequences but somehow it doesn't work. It works fine for nucleotide sequences but for proteins it just doesn't return any match (I used a sequence from the searched dataset, so there can't be no match). In the description it stands "This includes interfaces to blastn, blastp, blastx..." but in the help file in R studio it says "Description Execute blastn from blast+". Did anybody run rBlast for proteins?
Here's what I ran:
listF<-list.files("Trich_prot_fasta/")
fa<-paste0("Trich_prot_fasta/",listF[i])
makeblastdb(fa, dbtype = "prot", args="")
bl <- blast("Trich_prot_fasta/Tri5640_1_GeneModels_FilteredModels1_aa.fasta", type="blastp")
seq <- readAAStringSet("NDRkinase/testSeq.txt")
cl <- predict(bl, seq)
Result:
> cl <- predict(bl, seq)
Warning message: In predict.BLAST(bl, seq) : BLAST did not return a
match!
Tried to reproduce the error but everything worked as expected on my system (macOS BigSur 11.6 / R v4.1.1 / Rstudio v1.4.1717).
Given your blastn was successful, perhaps you are combining multiple fasta files for your protein fasta reference database? If that's the case, try concatenating them together and use the path to the file instead of an R object ("fa") when making your blastdb. Or perhaps:
makeblastdb(file = "Trich_prot_fasta/Tri5640_1_GeneModels_FilteredModels1_aa.fasta", type = "prot)
Instead of:
makeblastdb(fa, dbtype = "prot", args="")
Also, please edit your question to include the output from sessionInfo() (might help narrow things down).
library(tidyverse)
#BiocManager::install("Biostrings")
#devtools::install_github("mhahsler/rBLAST")
library(rBLAST)
# Download an example fasta file:
# https://ftp.uniprot.org/pub/databases/uniprot/current_release/knowledgebase/reference_proteomes/Eukaryota/UP000001542/UP000001542_5722.fasta.gz
# Grab the first fasta sequence as "example_sequence.fasta"
listF <- list.files("~/Downloads/Trich_example", full.names = TRUE)
listF
#> [1] "~/Downloads/Trich_example/UP000001542_5722.fasta"
#> [2] "~/Downloads/Trich_example/example_sequence.fasta"
makeblastdb(file = "~/Downloads/Trich_example/UP000001542_5722.fasta", dbtype = "prot")
bl <- blast("~/Downloads/Trich_example/UP000001542_5722.fasta", type = "blastp")
seq <- readAAStringSet("~/Downloads/Trich_example/example_sequence.fasta")
cl <- predict(bl, seq)
cl
#> QueryID SubjectID Perc.Ident Alignment.Length
#> 1 Example_sequence_1 tr|A2D8A1|A2D8A1_TRIVA 100.000 694
#> 2 Example_sequence_1 tr|A2E4L0|A2E4L0_TRIVA 64.553 694
#> 3 Example_sequence_1 tr|A2E4L0|A2E4L0_TRIVA 32.436 669
#> 4 Example_sequence_1 tr|A2D899|A2D899_TRIVA 64.344 488
#> 5 Example_sequence_1 tr|A2D899|A2D899_TRIVA 31.004 458
#> 6 Example_sequence_1 tr|A2D899|A2D899_TRIVA 27.070 314
#> 7 Example_sequence_1 tr|A2D898|A2D898_TRIVA 54.915 468
#> 8 Example_sequence_1 tr|A2D898|A2D898_TRIVA 33.691 653
#> 9 Example_sequence_1 tr|A2D898|A2D898_TRIVA 32.936 671
#> 10 Example_sequence_1 tr|A2D898|A2D898_TRIVA 29.969 654
#> 11 Example_sequence_1 tr|A2D898|A2D898_TRIVA 26.694 487
#> 12 Example_sequence_1 tr|A2D898|A2D898_TRIVA 25.000 464
#> 13 Example_sequence_1 tr|A2F4I3|A2F4I3_TRIVA 39.106 716
#> 14 Example_sequence_1 tr|A2F4I3|A2F4I3_TRIVA 30.724 677
#> 15 Example_sequence_1 tr|A2F4I3|A2F4I3_TRIVA 29.257 417
#> 16 Example_sequence_1 tr|A2F4I3|A2F4I3_TRIVA 23.438 640
#> 17 Example_sequence_1 tr|A2F4I3|A2F4I3_TRIVA 22.981 718
#> 18 Example_sequence_1 tr|A2F4I3|A2F4I3_TRIVA 24.107 112
#> 19 Example_sequence_1 tr|A2FI39|A2FI39_TRIVA 33.378 740
#> 20 Example_sequence_1 tr|A2FI39|A2FI39_TRIVA 31.440 722
#> Mismatches Gap.Openings Q.start Q.end S.start S.end E Bits
#> 1 0 0 1 694 1 694 0.00e+00 1402.0
#> 2 243 2 1 692 163 855 0.00e+00 920.0
#> 3 410 15 22 671 1 646 3.02e-94 312.0
#> 4 173 1 205 692 1 487 0.00e+00 644.0
#> 5 308 7 22 476 1 453 3.55e-55 198.0
#> 6 196 5 13 294 173 485 4.12e-25 110.0
#> 7 211 0 1 468 683 1150 8.48e-169 514.0
#> 8 420 11 2 647 501 1147 1.61e-91 309.0
#> 9 396 10 2 666 363 985 5.78e-89 301.0
#> 10 406 11 16 664 195 801 1.01e-66 238.0
#> 11 297 10 208 662 21 479 1.60e-36 147.0
#> 12 316 7 11 469 29 465 3.04e-36 147.0
#> 13 386 4 2 667 248 963 1.72e-149 461.0
#> 14 411 10 2 625 66 737 8.34e-83 283.0
#> 15 286 5 129 542 14 424 2.66e-52 196.0
#> 16 421 15 5 607 365 972 3.07e-38 152.0
#> 17 407 21 77 662 27 730 1.25e-33 138.0
#> 18 81 3 552 661 3 112 2.10e-01 35.4
#> 19 421 9 3 675 394 1128 1.12e-115 375.0
#> 20 409 15 2 647 163 874 1.21e-82 285.0
...
Created on 2021-09-30 by the reprex package (v2.0.1)
I have used lmlist to apply a common to grouped data (by Participant) and create a dataframe with each participant's coefficient (A), like so:
myCoefficients <- as.data.frame(coef(lmList(Outcome ~ A | Participant, mydata)))
This produces a dataframe that looks like this:
(Intercept) A
1 11.784913184 0.008224641
2 13.416552668 0.126538988
3 1.255375012 -0.261815119
4 -0.453204283 -0.050068500
5 1.855255007 0.019615941
6 18.233333333 0.266666667
7 10.622690151 0.039481512
8 4.583096557 0.056392969
9 -10.316190476 -0.327619048
10 -0.750918742 -0.011841568
11 0.000000000 0.000000000
12 1.658747938 -0.003200315
13 6.639959940 0.100150225
14 8.543573432 0.111088310
15 -0.409441233 0.075626204
16 0.000000000 0.000000000
17 16.391626950 0.338496534
18 -2.780630438 -0.119811724
19 3.581120944 0.057030482
20 -7.435064935 -0.086580087
21 0.000000000 0.000000000
22 -1.399803872 -0.041049967
23 0.000000000 0.000000000
24 5.748297340 0.073490890
25 2.502387775 0.022385387
26 5.477356181 -0.071399429
27 0.000000000 0.000000000
28 0.000000000 0.000000000
29 0.202822570 0.004236503
30 0.000000000 0.000000000
31 3.191532668 0.018318746
32 5.808669308 -0.089812308
33 15.556690047 0.288999378
34 6.044498212 0.033978540
35 5.384817738 0.088688463
36 2.429605338 -0.119020694
37 9.498121941 -0.068103350
38 1.449211455 -0.038649097
39 18.311828852 0.228294348
40 36.288255223 0.685770751
41 20.607074068 0.097268429
42 12.587294301 0.126299603
43 6.688188926 0.088422840
44 6.820835614 0.051997811
45 -2.063996902 -0.533215333
46 12.255847953 0.066520468
47 -4.818481848 -0.099009901
48 11.449166132 0.105355997
49 13.623012447 0.204422043
50 11.676916534 0.056099996
51 2.514750467 -0.019606775
52 0.125293117 -0.050347142
53 0.000000000 0.000000000
54 5.304979604 -0.175722320
55 10.318437929 0.096100382
56 0.000000000 0.000000000
57 10.607768097 -0.002279945
58 10.333968509 0.137398456
59 -4.513711889 -0.563639297
60 6.721815687 0.006860279
61 -0.718921180 -0.058085796
62 12.354598540 0.192189781
63 20.850979616 0.274787255
65 7.154075137 0.059849368
66 5.020082784 -0.008201748
67 0.229156161 -0.459531014
68 6.602570969 0.038832351
69 18.606677985 0.180531975
70 1.261939931 -0.064992614
71 0.000000000 0.000000000
72 8.565326633 0.105527638
73 6.025134650 0.065914337
74 0.411054480 -0.008632617
75 6.001972711 0.005753740
76 14.423697726 0.102622891
77 -1.782058047 -0.024274406
78 13.461871683 0.196417421
79 -2.490421456 -0.137691571
80 0.986939239 0.006814310
81 0.000000000 0.000000000
82 26.074865546 0.338946141
83 4.721769334 -0.023747076
84 3.491952414 0.055983205
85 8.621555769 0.111749489
86 13.298121427 0.175333515
87 5.075415244 -0.030621479
88 5.427200030 -0.056299149
89 5.784197111 0.052613361
90 2.967869893 -0.024593415
91 11.439869695 0.154194191
92 1.439169713 -0.137264690
93 0.000000000 0.000000000
94 5.696352440 0.077569872
95 2.544640478 -0.024518949
96 3.933483703 0.037261186
97 4.896524416 0.065283756
98 2.135022525 -0.374031801
99 7.190891371 0.083368454
100 23.054124552 0.531790023
101 6.161769255 -0.221620786
102 -14.547148289 -0.265139993
103 12.140619804 0.201380861
104 4.432939593 0.015699761
105 -0.837367221 -0.034716496
106 -0.122268163 -0.033668045
107 -5.696417101 -0.176646920
108 13.010822852 0.085412776
109 19.237564131 0.530002231
110 1.938517087 -0.178676770
111 6.888465629 -0.009047188
112 7.164846545 0.155902843
113 -0.403225806 -0.016129032
114 1.008421194 -0.058635633
115 3.170498084 0.028136973
116 7.475271328 0.008586344
117 9.387123820 0.130438620
118 -4.720329503 -0.102333977
119 -0.690008119 -0.122506379
120 9.928991185 -0.073457395
121 -8.166768363 -0.243770694
122 -0.836936555 -0.071781828
123 -13.756592007 -0.129834974
124 22.520513735 0.360226288
125 -6.268156425 -0.167597765
126 -7.517700552 -0.130074700
127 -14.041414611 -0.273382969
128 0.000000000 0.000000000
129 8.046064474 -0.259777450
130 3.669741697 -0.357933579
131 2.593244581 -0.190890087
132 -8.000000000 NA
133 8.328107184 -0.071265678
134 1.637694105 -0.192730521
135 3.693134192 -0.136592243
136 2.161687299 -0.180897599
137 0.000000000 0.000000000
138 0.721612005 -0.039410582
139 7.749737119 0.030494217
140 -5.808393153 -0.096355605
141 12.282297336 0.080170438
142 -5.316274128 -0.176288295
143 -4.441255140 -0.138249032
144 -1.117341518 -0.083225121
145 -0.752677582 -0.141942632
146 3.407083929 -0.101590819
147 6.265884172 0.002073376
148 -2.148945392 -0.152051430
149 28.415807560 0.554123711
150 8.716573171 0.118457600
151 12.496143959 0.088946015
152 19.149987332 0.217095262
153 0.009304822 -0.011094211
154 7.863488624 0.082204767
155 4.728299224 -0.416372618
156 4.624162598 0.035483138
157 8.492298276 0.117821075
158 -14.046175789 -0.229987520
159 1.183873993 0.017370732
160 0.000000000 0.000000000
161 7.045593921 0.066213394
162 5.513560300 0.031015580
163 4.244824402 -0.064298666
164 1.054528148 0.080908349
165 -11.086497890 -0.221518987
166 13.992083786 0.151239106
167 0.111296142 -0.093810713
168 -1.042661399 -0.045771688
169 16.828913325 0.164294955
170 1.743498818 -0.011820331
171 3.609741954 0.005798782
172 -2.110598319 0.011537828
173 -8.958189217 -0.714852492
174 -7.150518727 -0.141802105
175 10.645382098 0.141322541
176 -14.073564593 -0.445574163
177 16.096282103 0.289202798
178 8.649276860 0.162060950
179 0.000000000 0.000000000
180 -7.084494960 -0.158696118
181 -1.312672577 -0.103255341
182 -3.366945247 -0.223307037
183 14.938014695 0.208146358
184 -0.678981349 -0.185437590
185 10.027063876 0.030983707
186 12.853728050 0.126413533
187 -0.418968692 -0.010474217
188 7.242331839 0.175605381
189 -4.014336918 -0.143369176
190 10.229368645 0.118231260
191 -7.187500000 -0.539772727
192 25.785714286 0.464285714
193 15.189222323 0.136346677
194 12.845133231 0.164776481
195 -22.088160874 -0.266480224
196 0.000000000 0.000000000
197 0.000000000 0.000000000
198 1.362202049 -0.052177290
199 -11.184823000 -0.463622140
200 -0.859435330 -0.035683015
201 2.601550453 0.026826641
202 0.342018669 -0.125041972
204 5.335232543 0.084139393
205 20.000000000 NA
206 -0.375872383 -0.002492522
207 28.872131148 0.863934426
208 3.563577586 0.101939655
209 20.433887877 0.131835967
210 -5.815011124 -0.218884963
211 -11.266979501 -0.353173623
212 -11.173933501 -0.250941029
213 -0.056431693 -0.006460194
214 18.165829146 0.251256281
215 6.369295470 0.060584610
216 -3.322072072 -0.073198198
217 -2.677165354 0.078740157
218 0.898822629 -0.078491433
219 4.393285217 -0.030609615
220 6.165938069 0.054128719
221 15.223880597 0.179104478
222 22.583177570 0.337180008
223 15.386230059 0.304366079
224 -11.271916238 -0.118435454
225 9.214443626 0.087140752
226 13.651131824 -0.015978695
227 10.126607319 0.117705242
228 -4.100042391 -0.123781263
229 9.859298532 0.218189233
230 2.279411765 -0.489430147
231 7.042338217 0.066514042
232 -1.149144869 -0.020812374
233 3.501552079 -0.045179334
234 10.348929473 0.157197118
235 -4.309844723 -0.070597199
236 -0.406851091 0.064296520
237 0.000000000 0.000000000
238 -12.870644391 -0.185441527
239 6.572819573 0.091931533
240 146.000000000 7.800000000
241 59.560466246 1.131617290
242 -2.477952661 -0.017301454
243 7.670426344 0.195818054
244 4.464389692 0.030444692
245 0.000000000 0.000000000
246 -5.090371915 -0.281543274
247 3.318152764 0.031160115
248 2.089063523 -0.111329404
249 4.831575199 -0.003182445
250 3.528881195 0.044187318
251 -7.641223478 -0.186414990
252 8.691099476 0.314136126
253 7.673273132 0.161838865
254 11.152559139 0.074838199
255 -11.197466897 -0.077720207
256 1.761562470 0.020807718
257 12.047270872 0.192994389
258 3.223861983 0.014549990
259 -4.670398010 -0.061300640
260 1.468158966 0.014962892
261 3.374092944 0.074725953
262 0.000000000 0.000000000
263 11.308158651 0.131560838
264 30.000000000 NA
265 9.231769665 0.085299130
266 4.178069353 0.007966261
267 13.004952586 0.161534918
268 5.447510330 0.099796905
269 0.000000000 0.000000000
270 9.579288026 0.115580213
271 16.908915559 0.311615945
272 9.610721530 -0.059055346
273 9.265092417 0.141893316
274 -7.569751715 -0.282050043
275 5.496770989 -0.041728763
276 7.409235108 0.163905534
277 -1.593325458 -0.018606025
278 5.703958450 0.002105559
279 17.006838906 0.118920973
280 -3.320901995 0.143972246
281 30.029290374 0.623751831
282 -2.678965618 -0.022542195
283 24.687500000 0.312500000
284 -15.059602649 -0.238410596
286 0.000000000 NA
287 -2.157762938 -0.045909850
288 -7.806357888 -0.209365920
289 11.782270607 0.287713841
290 4.821551496 0.065395817
291 17.686923508 0.085167915
292 6.617959072 0.108308808
293 14.685806258 -0.169112143
294 11.424153771 0.142710499
295 2.513812155 0.041436464
296 19.254025911 0.275941397
297 11.448286941 0.142535853
298 -5.008457711 -0.162106136
299 -1.435754190 -0.140229741
300 23.478882079 0.311710847
The indexing column on the far left contains the Participant ID numbers. While there are only 297 rows in myCoefficients, the Participant IDs range from 1-300. This is because, for example, participant 285 and two others were removed from the analysis at an earlier stage.
How can I add empty rows were the missing participants are?
For example, where participant 285 is, I would like to see this:
...
283 24.687500000 0.312500000
284 -15.059602649 -0.238410596
285 NA NA
286 0.000000000 NA
287 -2.157762938 -0.045909850
...
Is there some way to use the indexing column, even though R doesn't recognise it as a column? i.e.:
ncol(myCoefficients)
[1] 2
As mentioned by #Simon Woodward, those are rownames, using tidyverse we can use complete to complete the missing rows with NA's.
library(tidyverse)
df %>%
rownames_to_column('row') %>%
mutate(row = as.integer(row)) %>%
complete(row = seq(min(row) : max(row))) %>%
select(-row)
# A tibble: 14 x 2
# A B
# <dbl> <dbl>
# 1 11.8 0.00822
# 2 13.4 0.127
# 3 1.26 -0.262
# 4 NA NA
# 5 -0.453 -0.0501
# 6 1.86 0.0196
# 7 18.2 0.267
# 8 10.6 0.0395
# 9 NA NA
#10 NA NA
#11 4.58 0.0564
#12 -10.3 -0.328
#13 -0.751 -0.0118
#14 0 0
data
Tested on this small subset of data
df <- structure(list(A = c(11.784913184, 13.416552668, 1.255375012,
-0.453204283, 1.855255007, 18.233333333, 10.622690151, 4.583096557,
-10.316190476, -0.750918742, 0), B = c(0.008224641, 0.126538988,
-0.261815119, -0.0500685, 0.019615941, 0.266666667, 0.039481512,
0.056392969, -0.327619048, -0.011841568, 0)), class = "data.frame", row.names = c("1",
"2", "3", "5", "6", "7", "8", "11", "12", "13", "14"))
Using dplyr and tibble:
library(dplyr)
library(tibble)
# list of participants
mydata <- tibble(n = 1:300)
# convert rownames to numeric column
myCoefficients <- myCoefficients %>%
rownames_to_column() %>%
mutate(rowname = as.numeric(rowname))
# join myCoefficients to participants
result <- left_join(mydata, myCoefficients, by = c("n" = "rowname"))
Here is a way:
library(dplyr)
seq_merge <- data.frame(a = seq(1:length(rownames(myCoefficients))))
myCoefficients$a <- rownames(myCoefficients)
final <- merge(seq_merge,myCoefficients,all.x = T) %>%
select(-a)
> final
(Intercept) A
1 11.784913184 0.008224641
2 13.416552668 0.126538988
3 1.255375012 -0.261815119
4 -0.453204283 -0.050068500
5 1.855255007 0.019615941
6 18.233333333 0.266666667
7 10.622690151 0.039481512
8 4.583096557 0.056392969
9 -10.316190476 -0.327619048
10 -0.750918742 -0.011841568
11 0.000000000 0.000000000
12 1.658747938 -0.003200315
13 6.639959940 0.100150225
14 8.543573432 0.111088310
15 -0.409441233 0.075626204
16 0.000000000 0.000000000
17 16.391626950 0.338496534
18 -2.780630438 -0.119811724
19 3.581120944 0.057030482
20 -7.435064935 -0.086580087
21 0.000000000 0.000000000
22 -1.399803872 -0.041049967
23 0.000000000 0.000000000
24 5.748297340 0.073490890
25 2.502387775 0.022385387
26 5.477356181 -0.071399429
27 0.000000000 0.000000000
28 0.000000000 0.000000000
29 0.202822570 0.004236503
30 0.000000000 0.000000000
31 3.191532668 0.018318746
32 5.808669308 -0.089812308
33 15.556690047 0.288999378
34 6.044498212 0.033978540
35 5.384817738 0.088688463
36 2.429605338 -0.119020694
37 9.498121941 -0.068103350
38 1.449211455 -0.038649097
39 18.311828852 0.228294348
40 36.288255223 0.685770751
41 20.607074068 0.097268429
42 12.587294301 0.126299603
43 6.688188926 0.088422840
44 6.820835614 0.051997811
45 -2.063996902 -0.533215333
46 12.255847953 0.066520468
47 -4.818481848 -0.099009901
48 11.449166132 0.105355997
49 13.623012447 0.204422043
50 11.676916534 0.056099996
51 2.514750467 -0.019606775
52 0.125293117 -0.050347142
53 0.000000000 0.000000000
54 5.304979604 -0.175722320
55 10.318437929 0.096100382
56 0.000000000 0.000000000
57 10.607768097 -0.002279945
58 10.333968509 0.137398456
59 -4.513711889 -0.563639297
60 6.721815687 0.006860279
61 -0.718921180 -0.058085796
62 12.354598540 0.192189781
63 20.850979616 0.274787255
64 NA NA
65 7.154075137 0.059849368
66 5.020082784 -0.008201748
67 0.229156161 -0.459531014
68 6.602570969 0.038832351
69 18.606677985 0.180531975
70 1.261939931 -0.064992614
71 0.000000000 0.000000000
72 8.565326633 0.105527638
73 6.025134650 0.065914337
74 0.411054480 -0.008632617
75 6.001972711 0.005753740
76 14.423697726 0.102622891
77 -1.782058047 -0.024274406
78 13.461871683 0.196417421
79 -2.490421456 -0.137691571
80 0.986939239 0.006814310
81 0.000000000 0.000000000
82 26.074865546 0.338946141
83 4.721769334 -0.023747076
84 3.491952414 0.055983205
85 8.621555769 0.111749489
86 13.298121427 0.175333515
87 5.075415244 -0.030621479
88 5.427200030 -0.056299149
89 5.784197111 0.052613361
90 2.967869893 -0.024593415
91 11.439869695 0.154194191
92 1.439169713 -0.137264690
93 0.000000000 0.000000000
94 5.696352440 0.077569872
95 2.544640478 -0.024518949
96 3.933483703 0.037261186
97 4.896524416 0.065283756
98 2.135022525 -0.374031801
99 7.190891371 0.083368454
100 23.054124552 0.531790023
101 6.161769255 -0.221620786
102 -14.547148289 -0.265139993
103 12.140619804 0.201380861
104 4.432939593 0.015699761
105 -0.837367221 -0.034716496
106 -0.122268163 -0.033668045
107 -5.696417101 -0.176646920
108 13.010822852 0.085412776
109 19.237564131 0.530002231
110 1.938517087 -0.178676770
111 6.888465629 -0.009047188
112 7.164846545 0.155902843
113 -0.403225806 -0.016129032
114 1.008421194 -0.058635633
115 3.170498084 0.028136973
116 7.475271328 0.008586344
117 9.387123820 0.130438620
118 -4.720329503 -0.102333977
119 -0.690008119 -0.122506379
120 9.928991185 -0.073457395
121 -8.166768363 -0.243770694
122 -0.836936555 -0.071781828
123 -13.756592007 -0.129834974
124 22.520513735 0.360226288
125 -6.268156425 -0.167597765
126 -7.517700552 -0.130074700
127 -14.041414611 -0.273382969
128 0.000000000 0.000000000
129 8.046064474 -0.259777450
130 3.669741697 -0.357933579
131 2.593244581 -0.190890087
132 -8.000000000 NA
133 8.328107184 -0.071265678
134 1.637694105 -0.192730521
135 3.693134192 -0.136592243
136 2.161687299 -0.180897599
137 0.000000000 0.000000000
138 0.721612005 -0.039410582
139 7.749737119 0.030494217
140 -5.808393153 -0.096355605
141 12.282297336 0.080170438
142 -5.316274128 -0.176288295
143 -4.441255140 -0.138249032
144 -1.117341518 -0.083225121
145 -0.752677582 -0.141942632
146 3.407083929 -0.101590819
147 6.265884172 0.002073376
148 -2.148945392 -0.152051430
149 28.415807560 0.554123711
150 8.716573171 0.118457600
151 12.496143959 0.088946015
152 19.149987332 0.217095262
153 0.009304822 -0.011094211
154 7.863488624 0.082204767
155 4.728299224 -0.416372618
156 4.624162598 0.035483138
157 8.492298276 0.117821075
158 -14.046175789 -0.229987520
159 1.183873993 0.017370732
160 0.000000000 0.000000000
161 7.045593921 0.066213394
162 5.513560300 0.031015580
163 4.244824402 -0.064298666
164 1.054528148 0.080908349
165 -11.086497890 -0.221518987
166 13.992083786 0.151239106
167 0.111296142 -0.093810713
168 -1.042661399 -0.045771688
169 16.828913325 0.164294955
170 1.743498818 -0.011820331
171 3.609741954 0.005798782
172 -2.110598319 0.011537828
173 -8.958189217 -0.714852492
174 -7.150518727 -0.141802105
175 10.645382098 0.141322541
176 -14.073564593 -0.445574163
177 16.096282103 0.289202798
178 8.649276860 0.162060950
179 0.000000000 0.000000000
180 -7.084494960 -0.158696118
181 -1.312672577 -0.103255341
182 -3.366945247 -0.223307037
183 14.938014695 0.208146358
184 -0.678981349 -0.185437590
185 10.027063876 0.030983707
186 12.853728050 0.126413533
187 -0.418968692 -0.010474217
188 7.242331839 0.175605381
189 -4.014336918 -0.143369176
190 10.229368645 0.118231260
191 -7.187500000 -0.539772727
192 25.785714286 0.464285714
193 15.189222323 0.136346677
194 12.845133231 0.164776481
195 -22.088160874 -0.266480224
196 0.000000000 0.000000000
197 0.000000000 0.000000000
198 1.362202049 -0.052177290
199 -11.184823000 -0.463622140
200 -0.859435330 -0.035683015
201 2.601550453 0.026826641
202 0.342018669 -0.125041972
203 NA NA
204 5.335232543 0.084139393
205 20.000000000 NA
206 -0.375872383 -0.002492522
207 28.872131148 0.863934426
208 3.563577586 0.101939655
209 20.433887877 0.131835967
210 -5.815011124 -0.218884963
211 -11.266979501 -0.353173623
212 -11.173933501 -0.250941029
213 -0.056431693 -0.006460194
214 18.165829146 0.251256281
215 6.369295470 0.060584610
216 -3.322072072 -0.073198198
217 -2.677165354 0.078740157
218 0.898822629 -0.078491433
219 4.393285217 -0.030609615
220 6.165938069 0.054128719
221 15.223880597 0.179104478
222 22.583177570 0.337180008
223 15.386230059 0.304366079
224 -11.271916238 -0.118435454
225 9.214443626 0.087140752
226 13.651131824 -0.015978695
227 10.126607319 0.117705242
228 -4.100042391 -0.123781263
229 9.859298532 0.218189233
230 2.279411765 -0.489430147
231 7.042338217 0.066514042
232 -1.149144869 -0.020812374
233 3.501552079 -0.045179334
234 10.348929473 0.157197118
235 -4.309844723 -0.070597199
236 -0.406851091 0.064296520
237 0.000000000 0.000000000
238 -12.870644391 -0.185441527
239 6.572819573 0.091931533
240 146.000000000 7.800000000
241 59.560466246 1.131617290
242 -2.477952661 -0.017301454
243 7.670426344 0.195818054
244 4.464389692 0.030444692
245 0.000000000 0.000000000
246 -5.090371915 -0.281543274
247 3.318152764 0.031160115
248 2.089063523 -0.111329404
249 4.831575199 -0.003182445
250 3.528881195 0.044187318
251 -7.641223478 -0.186414990
252 8.691099476 0.314136126
253 7.673273132 0.161838865
254 11.152559139 0.074838199
255 -11.197466897 -0.077720207
256 1.761562470 0.020807718
257 12.047270872 0.192994389
258 3.223861983 0.014549990
259 -4.670398010 -0.061300640
260 1.468158966 0.014962892
261 3.374092944 0.074725953
262 0.000000000 0.000000000
263 11.308158651 0.131560838
264 30.000000000 NA
265 9.231769665 0.085299130
266 4.178069353 0.007966261
267 13.004952586 0.161534918
268 5.447510330 0.099796905
269 0.000000000 0.000000000
270 9.579288026 0.115580213
271 16.908915559 0.311615945
272 9.610721530 -0.059055346
273 9.265092417 0.141893316
274 -7.569751715 -0.282050043
275 5.496770989 -0.041728763
276 7.409235108 0.163905534
277 -1.593325458 -0.018606025
278 5.703958450 0.002105559
279 17.006838906 0.118920973
280 -3.320901995 0.143972246
281 30.029290374 0.623751831
282 -2.678965618 -0.022542195
283 24.687500000 0.312500000
284 -15.059602649 -0.238410596
285 NA NA
286 0.000000000 NA
287 -2.157762938 -0.045909850
288 -7.806357888 -0.209365920
289 11.782270607 0.287713841
290 4.821551496 0.065395817
291 17.686923508 0.085167915
292 6.617959072 0.108308808
293 14.685806258 -0.169112143
294 11.424153771 0.142710499
295 2.513812155 0.041436464
296 19.254025911 0.275941397
297 11.448286941 0.142535853
In base you can use match to fill the missing rows like:
#Using length of x
y[match(seq_len(nrow(x)), rownames(y)),]
# i a
#1 1 5
#2 2 4
#NA NA NA
#4 4 2
#5 5 1
#Using rownames of y
y[match(min(as.numeric(rownames(y))):max(as.numeric(rownames(y))), rownames(y)),]
# i a
#1 1 5
#2 2 4
#NA NA NA
#4 4 2
#5 5 1
Data:
x <- data.frame(i=1:5, a=5:1)
y <- x[-3,]
y
# i a
#1 1 5
#2 2 4
#4 4 2
#5 5 1
Can any one help how to find approximate area under the curve using Riemann Sums in R?
It seems we do not have any package in R which could help.
Sample data:
MNo1 X1 Y1 MNo2 X2 Y2
1 2981 -66287 1 595 -47797
1 2981 -66287 1 595 -47797
2 2973 -66087 2 541 -47597
2 2973 -66087 2 541 -47597
3 2963 -65887 3 485 -47397
3 2963 -65887 3 485 -47397
4 2952 -65687 4 430 -47197
4 2952 -65687 4 430 -47197
5 2942 -65486 5 375 -46998
5 2942 -65486 5 375 -46998
6 2935 -65286 6 322 -46798
6 2935 -65286 6 322 -46798
7 2932 -65086 7 270 -46598
7 2932 -65086 7 270 -46598
8 2936 -64886 8 222 -46398
8 2936 -64886 8 222 -46398
9 2948 -64685 9 176 -46198
9 2948 -64685 9 176 -46198
10 2968 -64485 10 135 -45999
10 2968 -64485 10 135 -45999
11 2998 -64284 11 97 -45799
11 2998 -64284 11 97 -45799
12 3035 -64084 12 65 -45599
12 3035 -64084 12 65 -45599
13 3077 -63883 13 37 -45399
13 3077 -63883 13 37 -45399
14 3122 -63683 14 14 -45199
14 3122 -63683 14 14 -45199
15 3168 -63482 15 -5 -44999
15 3168 -63482 15 -5 -44999
16 3212 -63282 16 -20 -44799
16 3212 -63282 16 -20 -44799
17 3250 -63081 17 -31 -44599
17 3250 -63081 17 -31 -44599
18 3280 -62881 18 -38 -44399
18 3280 -62881 18 -38 -44399
19 3301 -62680 19 -43 -44199
19 3301 -62680 19 -43 -44199
20 3313 -62480 20 -45 -43999
Check this demo :
> library(zoo)
> x <- 1:10
> y <- -x^2
> Result <- sum(diff(x[x]) * rollmean(y[x], 2))
> Result
[1] -334.5
After check this question, I found function trapz() from package pracma be more efficient:
> library(pracma)
> Result.2 <- trapz(x, y)
> Result.2
[1] -334.5
I have started to work with species distribution modeling with GLM. Using BIOCLIM environmental data (for example: Bio10, Bio15, Bio16, Bio17 as predictors), the following data (stored in an object presausTrain):
ID bioclim_10 bioclim_11 bioclim_15 bioclim_16 pres longitude latitude
2 2 225.00000 105.00000000 22.206624 299.18014 1 -58.8786 -34.2269
3 3 228.97882 112.97809077 27.000000 319.94470 1 -59.5144 -33.7806
4 4 219.00000 104.57779206 16.000000 265.57779 1 -57.2555 -35.2549
6 6 188.00000 83.00000000 18.000000 260.42379 1 -57.5419 -38.0551
9 9 224.58419 104.73418836 23.000000 320.08305 1 -58.9186 -34.4132
10 10 243.60300 94.16917531 64.561824 85.17573 1 -68.6146 -32.8886
11 11 224.58433 104.73658836 23.000000 320.09025 1 -58.9187 -34.4133
12 12 253.00000 97.00000000 68.608231 71.99121 1 -68.5041 -32.3345
13 13 224.60863 104.75578836 23.000000 320.02305 1 -58.9195 -34.4128
15 15 245.44112 94.58706179 64.849824 84.25853 1 -68.6026 -32.8416
16 16 264.02281 151.00000000 54.022813 393.34787 1 -60.7727 -28.6506
17 17 244.67617 128.19141384 48.323829 366.28249 1 -60.6717 -31.6380
18 18 263.00000 149.49003689 53.490037 391.42668 1 -60.7500 -28.7500
19 19 272.04463 181.06767992 43.272909 412.80043 1 -58.1522 -25.1102
20 20 250.00000 132.00000000 49.877386 358.92412 1 -60.8829 -31.2539
21 21 268.54597 165.00000000 32.000000 418.09660 1 -58.0293 -28.0340
26 26 263.03251 149.36775948 53.286986 392.57182 1 -60.7333 -28.7333
27 27 262.00000 149.00000000 52.954712 394.07047 1 -60.6666 -28.7857
28 28 194.26954 91.54652958 113.000000 221.44775 1 -70.8308 -33.2159
29 29 195.00139 91.98381950 113.000000 219.30565 1 -70.8255 -33.2179
30 30 194.71515 92.34394042 113.000000 219.32903 1 -70.8312 -33.1968
31 31 194.87274 92.25693323 113.000000 218.64974 1 -70.8271 -33.2033
32 32 262.51488 149.00000000 53.000000 391.44238 1 -60.7334 -28.7999
33 33 236.09116 148.19977261 21.050265 543.87328 1 -53.9738 -25.8564
34 34 244.17649 128.15908399 47.077874 363.03794 1 -60.6339 -31.6890
36 36 249.80369 132.80368760 47.196312 364.22593 1 -60.2472 -31.2462
37 37 268.00000 164.88563766 32.000000 414.86622 1 -58.0654 -28.0482
38 38 268.00000 164.86220565 32.000000 414.68268 1 -58.0699 -28.0454
39 39 256.00000 142.51301366 48.000000 358.57247 1 -60.5333 -29.7500
40 40 255.02037 143.12581264 46.732643 438.70468 1 -59.7161 -29.3281
41 41 264.00000 151.00000000 54.000000 394.65955 1 -60.7500 -28.6500
42 42 254.54615 164.95675375 19.200389 502.30639 1 -54.4563 -25.6887
43 43 272.00000 173.71328176 36.025171 467.51253 1 -58.1000 -26.5833
44 44 286.97773 208.08168096 56.000000 292.08590 1 -59.5522 -21.2787
45 45 224.22325 78.22324976 38.606279 185.39521 1 -63.5337 -37.7471
46 46 248.74987 159.74987480 27.453648 559.43635 1 -54.2713 -25.6734
47 47 209.41746 124.45790111 107.988728 331.33831 1 -71.6073 -33.5050
48 48 244.38027 128.36415875 49.000000 369.61503 1 -60.6817 -31.5992
49 49 162.85989 96.36235347 118.491117 443.99917 1 -71.5244 -33.1645
50 50 130.32560 17.41336935 68.079547 360.58826 1 -71.1000 -40.9500
51 51 139.05510 25.70054673 69.765255 389.11327 1 -71.0837 -40.9810
52 52 209.13482 124.35046642 107.868234 332.58278 1 -71.6089 -33.5031
53 53 256.00458 165.33361100 21.301138 511.40500 1 -54.4162 -25.6967
54 54 271.00000 170.00000000 60.000000 362.54198 1 -60.4542 -25.9167
56 56 229.00000 112.35964626 25.000000 301.35039 1 -59.0210 -33.6877
57 57 119.99753 15.10747321 54.000000 471.71589 1 -71.7248 -42.7099
58 58 135.70071 20.70070732 72.827280 349.44457 1 -71.0065 -41.0595
59 59 264.00000 174.43120494 23.910081 420.64503 1 -57.0766 -26.0751
60 60 262.52382 173.72329246 25.077236 432.73019 1 -57.0500 -26.0167
62 62 179.34210 80.87832470 86.102021 594.32138 1 -72.6524 -37.8537
63 63 154.27204 63.26968212 83.647330 756.03579 1 -72.7667 -37.6333
64 64 170.36894 82.95671452 76.716261 582.33120 1 -72.9125 -38.0167
65 65 255.29339 141.05130937 44.000000 362.34977 1 -59.6919 -30.0224
68 68 244.00000 126.00000000 47.000000 373.97578 1 -60.7068 -31.8564
70 70 169.65447 81.75782454 60.138823 575.48334 1 -72.6000 -38.7333
71 71 280.00000 209.22244349 60.000000 311.98601 1 -60.0000 -20.0000
74 74 173.06376 91.94939494 86.649798 808.16328 1 -72.9333 -37.1667
75 75 93.88276 -3.88122756 123.993938 122.31361 1 -65.7049 -23.1626
77 77 244.73709 128.25037481 48.750699 368.01469 1 -60.7000 -31.6333
78 78 238.25716 118.42208981 26.120460 317.28934 1 -58.5249 -33.0121
79 79 264.68778 215.00000000 54.000000 469.93021 1 -63.0000 -17.0000
81 81 132.00000 77.00000000 37.000000 770.18289 1 -74.1167 -43.3500
82 82 204.24999 73.75029357 31.762202 275.78719 1 -60.2000 -37.3000
84 84 230.00000 113.03251305 23.367766 283.85559 1 -58.7333 -33.4833
85 85 239.68529 122.46175316 12.326192 327.46175 1 -55.7766 -32.5428
86 86 192.89750 78.09241708 19.000000 252.85173 1 -58.0658 -37.8406
87 87 127.72334 35.72334013 73.511696 1099.73574 1 -71.8167 -38.2167
90 90 225.43089 107.43089205 22.000000 304.29268 1 -58.3902 -34.8034
91 91 134.53429 72.02286008 40.000000 865.02286 1 -73.6167 -43.1167
92 92 225.07390 102.00000000 39.238986 337.55187 1 -60.7313 -34.2004
93 93 255.09615 141.09614673 15.688826 373.75971 1 -56.4500 -30.4300
95 95 168.91143 99.08857071 84.088571 593.54574 1 -73.0167 -36.7333
96 96 241.33689 219.33688825 75.000000 952.96431 1 -59.0000 -13.0000
97 97 267.51799 180.35046950 87.000000 353.46857 1 -63.0700 -20.8700
98 98 262.97274 210.03301311 61.289635 734.77698 1 -63.6667 -17.4500
99 99 217.00000 98.96529301 18.652335 283.29342 1 -57.9995 -35.5728
102 102 229.00000 107.00000000 28.000000 311.07590 1 -59.8228 -34.3834
104 104 225.00000 104.96487882 22.610418 318.55003 1 -59.0000 -34.4000
105 105 259.00000 147.04660936 24.512470 410.87977 1 -57.0944 -29.7149
107 107 244.31221 120.02550008 33.687788 366.17838 1 -59.0000 -31.8333
108 108 208.64289 87.07940941 14.000000 206.82547 1 -57.1347 -37.0029
109 109 248.30467 157.12855496 18.887519 493.23855 1 -54.3167 -25.9000
112 112 259.00000 151.00000000 22.000000 434.28496 1 -56.6444 -29.1753
113 113 227.87889 110.74950291 22.000000 310.87978 1 -58.3934 -34.7014
114 114 188.31179 83.92218970 17.311789 259.70139 1 -57.8431 -38.2656
116 116 224.69761 106.55401173 23.302389 327.10194 1 -58.5911 -34.4191
117 117 222.10785 105.00227249 25.889880 343.91302 1 -58.7179 -34.5725
118 118 200.28610 81.28609587 20.000000 248.04349 1 -58.2529 -37.8511
119 119 254.42257 162.36128278 19.840207 503.72360 1 -55.6030 -27.4438
120 120 249.80225 132.00000000 50.000000 358.77755 1 -61.0000 -31.0000
124 124 231.00000 142.00000000 15.000000 389.98814 1 -51.0936 -31.2872
126 126 235.43596 148.97624749 10.953114 404.58779 1 -50.9919 -29.9444
127 127 234.99430 153.85461324 11.073286 388.21653 1 -51.7181 -29.9433
128 128 233.60352 152.60352054 8.912486 383.80058 1 -51.3247 -29.7000
129 129 244.81880 184.81879611 42.716919 1025.62246 1 -46.4197 -23.8900
131 131 213.06989 159.02007109 60.544541 617.99241 1 -46.6339 -23.5503
133 133 212.30438 154.80780343 67.000000 636.61384 1 -46.8800 -23.1803
137 137 223.21176 165.93543578 70.980998 654.28145 1 -46.9797 -22.7003
22 2 194.00099 73.00099051 26.001083 276.00325 0 -59.1797 -37.3630
310 3 205.99766 62.99766278 20.000000 65.00000 0 -66.6797 -40.7797
410 4 267.99982 119.00012978 90.000314 163.00107 0 -67.0130 -30.1547
66 6 218.00083 127.00051598 16.000000 423.99824 0 -52.6380 -31.4047
8 8 272.99900 256.99786347 80.002135 769.99245 0 -48.2213 -10.5714
910 9 258.99943 245.00000000 20.999083 908.00078 0 -75.9297 -1.0297
1010 10 280.00232 267.00116165 86.000000 651.00936 0 -65.6380 8.3036
1110 11 279.00000 174.00000000 87.000000 465.99622 0 -63.2213 -23.1130
121 12 249.00582 217.00581833 70.999999 704.98944 0 -55.4297 -15.3214
14 14 273.00147 251.00146645 83.000000 809.99861 0 -51.3463 -12.9880
151 15 246.00666 221.00665863 85.001131 906.97968 0 -49.9713 -15.6964
161 16 263.00137 249.00250902 50.000000 835.00547 0 -71.0130 -8.1964
171 17 224.99969 99.99969124 43.000000 335.99883 0 -61.1797 -34.1547
181 18 228.99874 203.99940734 80.999335 669.99256 0 -47.6380 -15.4880
191 19 268.99981 254.99946750 98.000347 827.99056 0 -38.3880 -3.9047
201 20 76.98821 -0.01070989 17.000000 132.00969 0 -67.3463 -54.6964
25 25 229.00100 147.99999952 18.000000 521.00148 0 -53.5963 -25.3630
261 26 264.00251 247.00271798 54.000207 966.99373 0 -55.5130 0.1786
271 27 187.01335 46.01069668 35.998369 53.99662 0 -69.4713 -38.4047
281 28 228.00046 213.99999953 73.000000 815.00304 0 -60.7213 -12.6130
291 29 268.00058 262.00000000 45.000000 940.99818 0 -63.5547 -5.3630
301 30 228.01359 218.01360884 54.000000 1191.84478 0 -73.0547 9.4286
311 31 267.99977 257.99977009 12.001378 956.99770 0 -73.5130 -2.5714
321 32 253.00035 243.00034548 70.000000 897.99648 0 -51.8047 -4.6547
331 33 259.00000 242.99977322 58.000000 746.00023 0 -71.0130 -10.3214
35 35 266.00115 234.00023820 129.998376 343.98683 0 -80.1380 -2.8214
361 36 239.00091 158.00203076 9.999796 490.99858 0 -53.3880 -27.0714
371 37 256.00107 223.00214169 82.000471 710.99743 0 -49.6797 -18.4880
381 38 264.99942 250.99884783 29.998848 1096.99310 0 -70.4297 1.0536
391 39 15.01118 -12.98915222 81.002712 506.99793 0 -75.9713 -12.2797
401 40 259.00048 148.99941053 29.000000 407.99775 0 -57.8047 -29.1130
411 41 271.00000 261.99999933 39.998855 874.99310 0 -62.3047 -4.3214
421 42 245.99772 232.99637349 74.000000 1106.00622 0 -54.6380 -9.0714
431 43 270.00000 254.99884138 37.001159 1035.03518 0 -58.9713 5.6786
441 44 210.99715 171.99887466 77.000001 800.00466 0 -47.0547 -19.8214
451 45 258.99980 247.00000000 67.000000 783.00282 0 -53.5130 -4.5714
461 46 290.00000 265.00000000 85.999424 911.00865 0 -68.5547 7.2203
471 47 278.00101 181.99999886 68.998878 276.00123 0 -61.2213 -23.4464
481 48 262.99508 220.99508159 72.002646 313.03257 0 -36.8463 -9.9047
491 49 268.00000 247.99999990 59.000000 800.00140 0 -67.8880 -10.6130
501 50 248.99900 185.99899706 47.000000 460.00236 0 -52.2213 -22.2797
511 51 263.00095 228.00095374 92.001329 343.99639 0 -39.7630 -8.0714
521 52 266.00199 258.00132718 41.000444 959.00026 0 -65.0130 3.1370
531 53 251.00102 214.00036761 23.000651 262.00170 0 -39.2630 -12.6130
55 55 258.00020 248.00020222 74.999546 910.00354 0 -51.3880 -4.7380
561 56 223.00509 177.00376930 87.000000 680.00092 0 -42.7630 -18.0714
571 57 232.00792 162.00508410 91.000000 439.99713 0 -64.1797 -20.3214
581 58 122.97838 92.97610938 109.995439 423.17753 0 -73.3047 -15.0714
591 59 219.00298 160.00429666 73.998683 707.97064 0 -45.2213 -22.9047
61 61 278.00059 265.99955351 73.000000 994.99690 0 -60.5547 1.3870
621 62 264.99965 251.99965249 68.999652 931.99988 0 -54.5547 -3.0297
631 63 262.00057 252.99885628 31.000000 784.00057 0 -71.3047 -5.6964
641 64 276.00000 188.99909010 43.999678 324.00000 0 -58.9297 -23.7797
67 67 140.99782 16.99759906 42.000450 52.99995 0 -69.7630 -44.2380
69 69 91.02975 5.03139201 43.000502 727.18200 0 -72.5547 -43.3630
701 70 238.99996 150.99995612 22.999681 546.00091 0 -53.9297 -25.7797
73 73 156.99829 31.99828703 24.000743 55.00000 0 -70.3880 -48.0297
741 74 231.00043 215.00021260 75.000213 952.00132 0 -59.7630 -12.7380
76 76 187.00000 58.99890366 25.001097 58.00110 0 -65.6797 -43.5714
771 77 267.99832 252.99831923 105.998772 684.01876 0 -38.5547 -4.1130
781 78 269.99889 131.00022074 82.999779 177.00133 0 -66.3880 -30.1964
80 80 262.00102 254.00102180 41.000000 892.99807 0 -67.9713 -5.3214
811 81 255.00000 142.00000000 36.000000 376.99893 0 -58.1797 -29.7380
83 83 193.00076 91.00030596 94.000000 539.00085 0 -72.3880 -36.2797
841 84 278.99976 263.99975911 50.000000 1121.00142 0 -56.5547 -1.0297
851 85 250.00067 227.00067291 82.000000 917.00517 0 -55.3880 -13.7380
861 86 260.00000 250.99977489 23.000000 824.99751 0 -69.7630 -3.6130
89 89 282.00038 256.00019033 99.000532 413.00270 0 -41.8880 -7.5297
901 90 270.00000 225.99941948 57.998288 982.01583 0 -65.1797 -15.3214
911 91 267.99899 253.99898537 46.000000 491.01211 0 -61.5963 7.4703
921 92 219.00059 87.99889395 39.000000 319.99779 0 -61.3047 -36.3214
94 94 225.99978 86.99977939 72.999470 271.99907 0 -66.1380 -33.5297
951 95 116.00396 4.00263725 30.998909 74.99845 0 -71.5130 -48.0714
961 96 208.00089 134.00089034 13.000000 504.99850 0 -52.0130 -28.4880
971 97 259.99964 235.00031380 74.000666 869.99228 0 -57.2213 -14.6547
981 98 231.00000 79.00000000 49.999780 166.99961 0 -66.0130 -37.1547
101 101 245.99606 236.99707761 29.998848 1009.00190 0 -65.4713 1.5536
103 103 259.99783 246.99885797 54.001024 1039.00717 0 -68.7213 -7.5714
1041 104 244.00153 195.00152628 76.999074 658.99242 0 -49.1797 -21.0297
106 106 263.99796 253.99796462 71.001017 1446.01159 0 -64.2213 6.1786
1071 107 173.99788 103.99765761 173.996402 14.00164 0 -69.5130 -19.6130
1081 108 240.00781 232.00780741 34.000000 791.98642 0 -63.4713 3.1786
111 111 216.99799 105.99696451 108.002055 399.00018 0 -71.5963 -34.3630
1121 112 264.00119 249.00118836 57.000594 916.99807 0 -54.0547 1.1370
1131 113 265.99668 250.99667928 40.000000 687.99853 0 -74.0130 -8.0297
115 115 83.00514 44.00536119 101.999774 432.98950 0 -70.2630 -15.8630
1161 116 238.00000 118.00000000 17.000590 353.99879 0 -57.5547 -33.2797
1171 117 251.00441 219.00441215 91.999012 314.99232 0 -40.7213 -8.3214
1181 118 271.00022 262.00021686 33.000000 883.99764 0 -62.8880 -3.1964
1191 119 262.00000 252.99942668 25.000000 766.99989 0 -70.9297 -5.0297
123 123 276.00021 263.99964610 71.000215 887.00057 0 -61.5130 1.9703
125 125 247.99556 205.99494311 57.999540 274.00241 0 -36.8047 -9.1130
1261 126 249.99882 231.99882490 71.999271 452.98961 0 -67.4297 10.0120
1271 127 278.00000 236.00000000 69.000000 578.00059 0 -56.9297 -16.5714
1281 128 288.00021 198.99989033 65.999786 306.99978 0 -60.7630 -22.2797
130 130 283.00046 259.00091135 91.999846 722.00153 0 -42.1797 -5.7380
132 132 191.01856 177.01912717 68.003215 1108.93325 0 -73.5547 11.0953
136 136 261.00000 246.00102657 59.000000 726.00457 0 -71.2213 -9.6130
the expression for the model structure:
model <- pres ~ bioclim_10 + I(bioclim_10^2) + bioclim_11 + I(bioclim_11^2) + bioclim_15 + I(bioclim_15^2) + bioclim_16 + I(bioclim_16^2)
the following expression for GLM:
GLM <- glm(model, family=binomial(link=logit), data=presausTrain)
the results will contain a lot of negative values for the projected suitability for the species:
projecaoSuitability <- predict(predictors, GLM)
plot(projecaoSuitability, main='Myocastor coypus')
[
For example, at the coorddinates point -41.55306 -12.39342, the model predicts:
pointXY = data.frame(-41.55306, -12.39342)
suitabAtPoint = extract(predictors,pointXY)
predictedSuitabilityAtPoint = predict(GLM, as.data.frame(suitabAtPoint))
-2.167515
I think that negative values is occouring because some mistake of mine, as it make non sense and my Random Forest, Maxent and Bioclim returns values ranging from 0 to 1.
Someone can help me, please?
You need to use the type = 'response' argument in your call of predict (the default is link). This will give you fitted probabilities rather than their natural logs.
So, I have these data given below, and my goal is to aggregate column v3 in terms of columns v1 and v2 and add the v3 values for each bin of v1 and v2. For example, the first line correspond to interval v1=21, v2=16, so the value of v3 will be aggregated over its (v1,v2) interval. And repeat this for the rest of rows. I want to use the mean as the aggregation function!
> df
v1 v2 v3
1 21.359 16.234 24.283
2 47.340 9.184 21.328
3 35.363 -13.258 14.556
4 -29.888 14.154 17.718
5 -10.109 -16.994 20.200
6 -32.387 1.722 15.735
7 49.240 -5.266 17.601
8 -38.933 2.558 16.377
9 41.213 5.937 21.654
10 -33.287 -4.028 19.525
11 -10.223 11.961 16.756
12 -48.652 16.558 20.800
13 44.778 27.741 17.793
14 -38.546 29.708 13.948
15 -45.622 4.729 17.793
16 -36.290 12.383 18.014
17 -19.626 19.767 18.182
18 -32.248 29.480 15.108
19 -41.859 35.502 8.490
20 -36.058 21.191 16.714
21 -23.588 0.524 21.471
22 -24.423 39.963 18.257
23 -0.042 -45.899 17.654
24 -35.479 32.049 9.294
25 -24.632 20.603 17.757
26 -26.591 25.882 18.968
27 -34.364 43.959 13.905
28 -19.334 29.728 20.102
29 12.304 -39.997 17.002
30 0.958 37.162 20.779
31 -35.475 -40.611 14.719
32 -39.268 44.382 11.247
33 -10.154 39.053 19.458
34 -12.612 32.056 17.759
35 2.730 -1.473 20.228
36 -45.326 -52.299 9.305
37 -1.996 -15.551 13.295
38 -26.655 -37.319 19.148
39 -18.509 -30.047 18.889
40 -22.705 -25.577 19.007
41 -15.705 -15.397 19.112
42 -2.637 9.790 10.548
43 -14.107 -3.145 19.654
44 -29.272 -19.906 18.503
45 -9.569 -4.632 11.334
46 2.114 18.048 14.744
47 -4.241 16.073 15.420
48 31.869 -3.394 21.559
49 20.425 35.205 22.250
50 -18.605 -8.866 20.082
51 -26.677 -7.690 21.850
52 -5.240 4.805 11.399
53 -6.766 2.538 6.292
54 4.567 22.554 19.682
55 -20.701 6.430 20.996
56 -23.972 16.141 17.976
57 -6.651 24.048 18.082
58 -32.243 -6.100 19.517
59 2.236 29.736 19.667
60 18.830 15.586 15.969
61 -9.598 28.414 17.806
62 -30.825 12.194 22.346
63 -17.415 15.795 18.135
64 -14.823 5.931 17.915
65 -14.234 12.882 13.001
66 9.937 18.368 20.421
67 -38.766 9.590 21.648
68 -30.896 27.047 16.453
69 -4.432 -10.562 10.061
70 -4.290 33.170 22.942
71 7.285 41.416 23.906
72 24.411 40.531 23.584
73 45.409 -32.420 20.831
74 49.341 -34.047 15.269
75 -7.730 -47.724 21.692
76 -10.563 -29.082 17.984
77 4.412 -41.182 16.845
78 31.822 -37.297 19.665
79 -43.355 31.093 17.688
80 -44.353 -44.723 13.832
81 -16.961 38.438 20.715
82 -21.225 -39.244 18.156
83 -42.022 -8.686 20.362
84 -42.904 -25.498 18.394
85 43.822 -25.990 21.287
86 43.013 -9.071 19.285
87 -36.901 -24.185 21.938
88 -28.251 -36.583 19.330
89 -19.830 -22.412 21.677
90 -3.789 -15.663 17.439
91 40.453 -21.796 17.432
92 -40.778 -31.188 18.762
93 -27.072 -48.609 18.913
94 -18.035 -1.791 19.909
95 -20.781 -7.912 22.563
96 47.307 -15.432 19.101
97 30.700 5.097 22.801
98 46.453 0.171 17.810
99 -27.439 -5.860 22.626
100 -30.526 -18.007 23.219
101 -18.280 -15.187 25.302
102 -18.367 6.044 18.864
103 41.265 -1.686 22.743
104 29.227 -14.814 19.196
105 -36.080 -32.715 18.930
106 7.475 7.061 25.002
107 -18.586 -45.207 21.864
108 35.227 11.148 21.388
109 -7.581 38.773 22.048
110 -43.685 14.083 22.037
111 -29.533 39.735 17.613
112 8.760 -39.400 22.421
113 -14.962 24.624 12.030
114 18.627 -32.888 23.036
115 -31.300 33.612 15.608
116 -38.024 45.839 16.567
117 -15.104 36.893 18.162
118 -12.809 -23.029 21.589
119 -21.614 36.264 16.680
120 42.917 -36.838 18.738
121 6.104 -14.961 14.468
122 44.032 -41.556 17.618
123 -24.493 21.886 17.366
124 -24.361 29.941 14.374
125 -25.060 43.383 16.437
126 -6.017 -24.640 19.207
127 -32.617 -40.549 18.059
128 -43.285 -43.364 18.827
129 -29.856 -46.089 16.881
130 -16.547 -43.619 22.547
131 -16.257 42.814 18.932
132 -9.236 -11.694 14.455
133 13.488 -35.422 24.436
134 -47.456 -32.714 18.123
135 39.476 -28.008 16.087
136 -21.933 -43.522 15.390
137 -17.347 -38.250 16.738
138 -4.948 -39.747 21.598
139 -31.018 -28.912 21.332
140 -36.364 30.461 17.542
141 -39.639 18.272 23.663
142 -24.162 -13.582 19.136
143 -8.935 -32.699 22.108
144 0.001 -19.219 17.888
145 -6.912 -24.885 20.683
146 7.785 -31.229 15.972
147 22.176 -7.478 21.335
148 8.755 -13.323 20.831
149 44.081 41.160 11.938
150 -8.451 -37.721 17.465
151 18.671 -2.776 23.374
152 12.668 -26.749 18.071
153 1.582 -21.252 20.750
154 20.832 -27.718 16.190
155 44.220 -45.690 12.598
156 -0.226 -37.737 17.634
157 -25.130 -19.197 23.170
158 2.086 -31.271 18.180
159 -20.445 -33.083 19.984
160 23.801 1.116 24.230
161 18.283 -17.922 20.256
162 -38.985 -13.770 20.702
163 -26.264 -27.413 20.276
164 10.396 -19.375 20.415
165 -16.343 -22.847 16.516
166 29.992 -8.215 21.661
167 35.052 -19.475 16.953
168 3.052 -6.800 22.509
169 -10.350 -5.413 19.222
170 14.371 -10.383 23.471
171 11.896 -4.191 21.773
172 18.152 8.741 23.669
173 25.748 -47.786 18.578
174 31.613 -0.735 23.898
175 12.660 25.645 23.549
176 2.933 29.345 25.170
177 9.369 18.791 26.817
178 15.805 4.798 27.866
179 27.556 -25.571 14.796
180 -5.112 -7.835 21.201
181 -30.571 3.471 20.496
182 19.816 -22.114 21.210
183 2.826 47.437 22.911
184 25.488 -33.064 21.442
185 44.826 42.162 22.994
186 25.208 -48.487 25.325
187 14.635 -17.430 17.083
188 -1.901 -33.370 22.163
189 12.306 -47.265 20.052
190 42.552 35.750 23.213
191 37.318 -46.069 22.599
192 4.725 -22.289 21.600
193 -40.815 -37.793 17.371
194 11.890 -12.862 14.286
195 35.251 -31.746 17.816
196 27.121 -27.638 19.677
197 36.024 -39.105 20.202
198 -47.119 41.940 17.526
199 0.837 -40.694 23.063
200 23.797 -39.795 20.198
201 -42.859 -21.372 23.554
202 39.407 -20.211 21.246
203 25.782 -18.892 20.423
204 34.529 -9.576 20.411
205 44.397 -13.247 23.180
206 5.534 6.856 14.248
207 31.598 -18.085 22.350
208 7.250 -0.481 15.453
209 -43.458 -15.204 23.193
210 -38.296 -31.524 21.776
211 4.276 -3.483 12.145
212 25.757 -11.708 22.360
213 15.634 37.478 24.624
214 -43.669 -3.197 20.742
215 45.381 6.365 21.351
216 -38.755 -6.877 20.879
217 -6.925 3.994 21.120
218 8.059 12.831 26.032
219 3.572 22.105 26.920
220 16.042 30.267 21.039
221 26.629 13.042 23.633
222 -12.126 -0.151 21.261
223 -11.981 24.600 19.236
224 29.480 28.362 21.838
225 -2.500 22.858 23.177
226 -41.163 19.863 20.059
227 35.953 27.401 19.101
228 -16.641 13.248 17.984
229 -3.778 14.090 18.943
230 11.643 34.817 21.621
231 34.921 38.666 17.359
232 25.621 22.451 22.866
233 34.936 17.384 19.836
234 40.017 37.599 13.987
235 19.547 33.838 22.575
236 11.197 39.977 19.347
237 16.972 -33.927 14.205
238 22.938 38.064 20.351
239 40.234 18.672 23.030
240 -0.846 42.320 18.383
241 -11.437 18.284 16.502
242 19.552 43.222 21.370
243 13.925 -46.486 18.917
244 41.709 -39.559 16.143
245 19.014 -44.563 17.796
246 32.260 33.114 18.402
247 -4.693 29.228 18.622
248 21.765 -38.452 15.147
249 39.157 -31.135 19.800
250 32.638 46.241 18.943
251 2.797 10.089 21.330
252 8.256 46.910 18.834
253 38.634 -2.429 20.413
254 28.642 2.763 19.580
255 0.456 1.422 7.452
256 3.050 11.792 14.196
257 24.736 14.532 17.886
258 16.787 -10.155 18.607
259 12.676 11.651 18.656
260 13.184 1.081 15.385
261 27.365 26.576 25.486
262 -7.878 -18.191 14.547
263 -42.112 32.576 20.865
264 15.069 21.684 17.986
265 33.045 27.166 25.252
266 21.810 -0.186 19.477
267 18.227 26.690 20.415
268 33.759 18.366 21.255
269 39.491 13.272 23.036
270 30.662 9.368 20.192
271 5.470 35.303 22.685
272 21.663 -44.343 20.999
273 31.261 33.178 24.335
274 21.854 22.665 20.876
275 21.853 7.932 18.588
276 -40.168 3.682 19.642
277 -42.292 23.997 22.199
278 10.233 28.731 21.263
279 17.745 41.831 19.536
280 38.406 25.165 26.534
281 -49.329 -0.465 20.887
282 40.398 -8.120 21.362
283 -2.531 46.118 22.933
284 7.959 -30.856 20.497
285 -34.467 -23.724 22.206
286 30.541 44.284 25.878
287 45.682 29.897 21.964
288 -22.251 -0.089 20.756
289 21.484 16.532 23.513
290 46.912 10.195 21.908
291 35.320 -13.352 16.102
292 -30.431 14.048 17.362
293 -8.976 -17.325 21.645
294 -32.661 2.301 16.805
295 49.317 -5.509 17.711
296 -37.756 4.459 16.054
297 41.445 6.158 21.442
298 -33.148 -3.499 19.543
299 -10.065 12.238 16.649
300 -48.323 17.153 20.974
301 45.010 28.147 17.838
302 -39.630 29.183 13.254
303 -45.191 5.065 18.214
304 -35.936 11.953 16.540
305 -19.816 19.624 18.279
306 -32.055 29.757 15.358
307 -41.533 36.169 10.005
308 -35.448 20.960 16.720
309 -23.384 0.511 20.005
310 -25.101 40.569 18.180
311 -0.547 -45.779 17.603
312 -35.291 32.643 9.548
313 -25.109 20.826 17.494
314 -26.202 27.012 18.678
315 -34.805 43.850 14.006
316 -18.819 30.611 20.309
317 13.019 -40.248 16.874
318 -0.655 37.112 20.924
319 -34.142 -41.553 15.237
320 -39.509 43.886 12.464
321 -9.491 38.639 18.839
322 -12.164 31.977 17.598
323 3.437 -1.596 20.318
324 -45.713 -52.599 9.918
325 -2.062 -15.946 12.847
326 -27.435 -37.600 18.257
327 -18.094 -29.624 18.791
328 -22.647 -26.123 18.746
329 -16.775 -15.505 19.204
330 -2.628 9.599 11.219
331 -15.718 -1.797 19.491
332 -29.476 -20.107 17.485
333 -10.618 -4.938 12.227
334 1.423 17.458 14.706
335 -4.503 16.630 14.718
336 32.450 -2.029 21.591
337 20.529 35.464 21.630
338 -19.348 -7.844 19.464
339 -26.760 -6.856 21.422
340 -4.539 4.393 11.819
341 -5.741 1.934 7.121
342 4.781 21.919 18.908
343 -19.797 6.928 20.928
344 -24.555 16.834 19.796
345 -5.664 24.465 18.432
346 -32.891 -6.571 18.691
347 2.354 28.462 19.825
348 18.058 16.251 16.335
349 -9.603 28.582 17.743
350 -31.282 11.454 22.342
351 -17.580 16.428 18.401
352 -13.884 6.206 17.270
353 -13.631 13.767 11.761
354 9.712 18.008 18.896
355 -37.987 9.024 21.309
356 -29.969 27.506 16.964
357 -4.248 -10.813 9.284
358 -5.755 32.673 22.541
359 6.675 41.952 24.227
360 24.564 41.173 23.241
361 45.314 -32.299 20.778
362 -45.890 -33.510 16.314
363 -8.277 -47.943 21.573
364 -11.044 -29.464 17.708
365 3.972 -41.396 17.411
366 31.776 -36.643 19.998
367 -43.072 31.311 17.828
368 -45.805 -43.071 14.477
369 -15.628 39.837 19.709
370 -21.129 -39.101 18.814
371 -41.628 -8.980 19.850
372 -42.244 -23.659 18.856
373 44.149 -25.710 21.099
374 42.623 -9.185 20.147
375 -35.949 -23.979 22.255
376 -28.512 -36.367 19.378
377 -19.827 -21.781 21.621
378 -3.429 -15.706 18.677
379 39.741 -20.721 18.670
380 -41.663 -29.499 19.260
381 -26.931 -48.467 18.185
382 -17.571 -1.467 19.770
383 -20.039 -7.591 22.737
384 46.370 -14.790 19.922
385 30.710 4.167 22.987
386 46.755 0.417 18.088
387 -27.293 -4.398 22.168
388 -30.364 -17.573 23.869
389 -16.870 -14.893 25.817
390 -18.152 6.546 18.392
391 40.134 0.160 23.661
392 28.179 -14.323 19.301
393 -35.907 -32.647 19.306
394 8.486 7.101 24.551
395 -17.155 -45.435 22.745
396 34.226 10.748 19.773
397 -7.760 38.754 22.211
398 -42.899 13.804 22.628
399 -29.972 40.435 17.784
400 8.764 -39.195 22.070
401 -15.624 25.585 12.291
402 18.620 -33.314 23.282
403 -30.436 34.219 15.102
404 -37.665 44.955 15.257
405 -15.861 37.488 18.956
406 -13.375 -22.408 20.312
407 -20.972 36.906 17.387
408 43.162 -35.948 19.695
409 6.639 -15.783 14.608
410 44.186 -41.037 17.398
411 -23.917 22.236 18.702
412 -23.957 30.033 14.725
413 -25.056 43.824 15.489
414 -6.795 -24.375 18.537
415 -33.485 -40.651 17.538
416 -43.186 -43.071 17.481
417 -30.325 -46.122 16.440
418 -17.489 -43.551 22.006
419 -16.376 43.928 18.992
420 -9.076 -10.921 14.131
421 13.704 -36.352 23.812
422 -47.302 -31.918 18.719
423 39.459 -27.814 15.558
424 -22.509 -42.660 14.366
425 -17.920 -37.614 16.572
426 -5.780 -39.212 21.667
427 -30.519 -28.942 21.931
428 -35.937 31.435 17.106
429 -38.680 18.435 23.342
430 -24.796 -13.279 18.543
431 -9.283 -32.388 21.895
432 0.493 -19.505 17.276
433 -7.046 -25.243 20.741
434 7.884 -32.006 16.727
435 22.451 -7.834 21.082
436 8.379 -13.690 22.002
437 43.730 41.697 11.894
438 -9.040 -38.086 17.500
439 18.831 -2.759 23.252
440 12.732 -27.410 18.948
441 0.739 -21.091 21.354
442 20.339 -27.959 16.514
443 44.688 -46.449 12.356
444 -0.402 -36.951 17.891
445 -24.790 -18.139 23.337
446 2.173 -30.577 18.023
447 -18.995 -33.799 20.730
448 23.372 0.223 24.855
449 17.835 -17.372 19.878
450 -38.915 -13.815 20.923
451 -26.241 -27.800 19.877
452 11.074 -18.156 19.249
453 -16.478 -22.928 16.386
454 29.646 -8.349 21.115
455 33.910 -20.809 16.629
456 3.306 -6.830 22.059
457 -10.512 -5.322 19.876
458 14.024 -10.406 23.456
459 12.365 -3.699 21.818
460 18.186 8.532 23.951
461 25.140 -47.653 18.592
462 32.288 -2.117 23.423
463 10.836 24.937 23.310
464 4.531 28.913 25.238
465 9.944 18.397 26.661
466 16.274 4.852 27.837
467 27.316 -26.007 15.934
468 -4.508 -8.010 20.906
469 -29.858 2.412 19.958
470 20.376 -21.957 21.306
471 2.077 47.431 23.248
472 25.777 -33.367 21.695
473 44.854 42.801 22.904
474 25.356 -48.833 25.402
475 15.322 -16.926 17.318
476 -2.656 -33.400 20.365
477 11.950 -47.390 20.328
478 42.961 36.955 22.919
479 35.726 -45.402 24.272
480 4.675 -21.758 21.780
481 -40.568 -36.931 16.934
482 11.758 -12.859 14.206
483 35.483 -31.760 16.975
484 27.336 -27.577 19.429
485 36.689 -39.218 19.668
486 -46.357 41.618 17.456
487 0.002 -40.589 22.558
488 23.525 -39.918 21.247
489 -43.269 -21.304 22.699
490 40.191 -20.594 21.145
491 25.728 -18.024 20.298
492 34.964 -10.441 20.189
493 43.627 -13.279 23.038
494 5.766 6.876 14.077
495 32.432 -18.172 21.848
496 7.087 -1.122 15.098
497 -44.110 -14.034 23.080
498 -39.474 -31.289 22.312
499 4.118 -4.077 11.067
500 26.597 -11.667 22.641
so, using these commands I can find the intervals, as below
x.bin <- seq(floor(min(d[,1])), ceiling(max(df[,1])), by=2)
y.bin <- seq(floor(min(d[,2])), ceiling(max(df[,2])), by=2)
> x.bin
[1] -50 -48 -46 -44 -42 -40 -38 -36 -34 -32 -30 -28 -26 -24 -22 -20 -18 -16 -14
[20] -12 -10 -8 -6 -4 -2 0 2 4 6 8 10 12 14 16 18 20 22 24
[39] 26 28 30 32 34 36 38 40 42 44 46 48 50
> y.bin
[1] -53 -51 -49 -47 -45 -43 -41 -39 -37 -35 -33 -31 -29 -27 -25 -23 -21 -19 -17
[20] -15 -13 -11 -9 -7 -5 -3 -1 1 3 5 7 9 11 13 15 17 19 21
[39] 23 25 27 29 31 33 35 37 39 41 43 45 47
But, then I don't know how to assign each row of the raw data (df) to each x.bin and y.bin and calculate the aggregate (sum) of each bin.
library(plyr)
#I am using cut function with 50 breaks for both v1 and v2 and ddply from plyr package for computing the mean
newdata<-ddply(df,.(cut(v1,50),cut(v2,50)),summarise,mean.v3=mean(v3))
> head(newdata)
cut(v1, 50) cut(v2, 50) mean.v3
1 (-49.4,-47.5] (-34.7,-32.7] 18.123
2 (-49.4,-47.5] (-0.576,1.43] 20.887
3 (-49.4,-47.5] (15.5,17.5] 20.887
4 (-47.5,-45.5] (-52.7,-50.7] 9.918
5 (-47.5,-45.5] (-44.7,-42.7] 14.477
6 (-47.5,-45.5] (-34.7,-32.7] 16.314
Updated as per the comments: If you want the lower, middle and mid-points, you can use the following function or use with details as follow(you need to use the sub function to deal with ( and ]):
df$newv1<-with(df,cut(v1,50))
df$newv2<-with(df,cut(v2,50))
df$lowerv1<-with(df,as.numeric( sub("\\((.+),.*", "\\1", newv1))) #lower value
df$upperv1<-with(df,as.numeric( sub("[^,]*,([^]]*)\\]", "\\1", newv1))) # upper value
df$midv1<-with(df,(lowerv1+upperv1)/2) #mid value
df$lowerv2<-with(df,as.numeric( sub("\\((.+),.*", "\\1",newv2))) #lower value
df$upperv2<-with(df,as.numeric( sub("[^,]*,([^]]*)\\]", "\\1", newv2))) # upper value
df$midv2<-with(df,(lowerv2+upperv2)/2)#mid value
newdata<-ddply(df,.(newv1,newv2),transform,mean.v3=mean(v3))
> head(newdata)
v1 v2 v3 newv1 newv2 lowerv1 upperv1 midv1 lowerv2 upperv2 midv2 mean.v3
1 -47.456 -32.714 18.123 (-49.4,-47.5] (-34.7,-32.7] -49.4 -47.5 -48.45 -34.700 -32.70 -33.700 18.123
2 -49.329 -0.465 20.887 (-49.4,-47.5] (-0.576,1.43] -49.4 -47.5 -48.45 -0.576 1.43 0.427 20.887
3 -48.652 16.558 20.800 (-49.4,-47.5] (15.5,17.5] -49.4 -47.5 -48.45 15.500 17.50 16.500 20.887
4 -48.323 17.153 20.974 (-49.4,-47.5] (15.5,17.5] -49.4 -47.5 -48.45 15.500 17.50 16.500 20.887
5 -45.713 -52.599 9.918 (-47.5,-45.5] (-52.7,-50.7] -47.5 -45.5 -46.50 -52.700 -50.70 -51.700 9.918
6 -45.805 -43.071 14.477 (-47.5,-45.5] (-44.7,-42.7] -47.5 -45.5 -46.50 -44.700 -42.70 -43.700 14.477