How to delete a single cell? - r

So, I am trying to delete a single cell from my data. The problem is that a normal function doesn't work:
data[-1,]
data[-1,-1]
data$x = NULL
etc
So that's how my main code looks like:
data_rd <- data_rd[-1,] ## I could delete one row but can't the next one.
## Create an empty matrix
name_cols <- paste0("F", rep(1:20, each = 4), "_", 1:4)
name_columns <- c("Description", name_cols)
mat_master_EOD <- matrix(0, nrow = length(data_rd[,1]),ncol = 81)
colnames(mat_master_EOD) <- name_columns
rownames(mat_master_EOD) <- data_rd[,1]
The cell which I want to delete:
http://imageshack.com/a/img32/6227/hy0s.jpg
I used rbind.fillfunction to group some of data files, that might be the reason why I am not able to remove this cell.
EDIT:
That's only what I can do for you:
dput(data_rd[1:3, 1:3])
structure(list(X = c("Accession", "ZZ_FGCZCont0025", "ZZ_FGCZCont0099"
), X.1 = structure(c(29L, 22L, 20L), .Label = c("", "1", "10",
"11", "12", "13", "14", "15", "16", "17", "18", "19", "2", "20",
"21", "23", "25", "27", "28", "29", "3", "34", "4", "5", "6",
"7", "8", "9", "Peptide count", "22", "24", "26", "30", "31",
"32", "33", "35", "36", "37", "40", "41", "45", "46", "50", "55",
"63", "73", "38", "48", "56", "68", "82", "39", "42", "43", "44",
"58", "67", "76", "90", "59", "49", "54", "71", "65", "51", "53",
"57", "61", "64", "74", "75", "47", "52", "60", "72", "69", "70",
"62"), class = "factor"), X.2 = structure(c(23L, 9L, 15L), .Label = c("",
"0", "1", "10", "11", "12", "13", "14", "15", "16", "17", "18",
"19", "2", "28", "3", "4", "5", "6", "7", "8", "9", "Peptides used for quantitation",
"20", "21", "22", "23", "24", "25", "26", "27", "29", "30", "33",
"37", "39", "41", "45", "46", "49", "32", "34", "35", "36", "44",
"68", "40", "43", "47", "66", "88", "42", "59", "58", "52", "71",
"31", "38", "51", "48", "50", "62"), class = "factor")), .Names = c("X",
"X.1", "X.2"), row.names = 2:4, class = "data.frame")
And for the new created matrix:
> dput(mat_master_EOD[1:10, 1:10])
structure(c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), .Dim = c(10L,
10L), .Dimnames = list(c("Accession", "ZZ_FGCZCont0025", "ZZ_FGCZCont0099",
"ZZ_FGCZCont0126", "ZZ_FGCZCont0146", "AT1G19570", "ZZ_FGCZCont0158",
"AT5G38480", "ZZ_FGCZCont0050", "AT1G07370"), c("Description",
"F1_1", "F1_2", "F1_3", "F1_4", "F2_1", "F2_2", "F2_3", "F2_4",
"F3_1")))

I'm reaching here, but is this is what you're trying to get:
Accession Peptide count Peptides used for quantitation
3 ZZ_FGCZCont0025 34 15
4 ZZ_FGCZCont0099 29 28
If so, you can do this:
setNames(data_rd[-1,], sapply(data_rd[1, ], as.character))
Note that your Peptide... columns are factors, which is terribly confusing for numeric columns. You should consider re-importing the data using something like read.csv(file="blahblah", header=TRUE) which I think will resolve most your problems from the get go.

Related

Error in as.vector(data) when creating a mask for SECR

I am trying to create a mask in SECR using a .shp file. Always getting this error: Error in as.vector(data) :
no method for coercing this S4 class to a vector when i use the make.mask
this is my code:
fence <- rgdal :: readOGR('/Rdata/SECR', layer = 'building')
OGR data source with driver: ESRI Shapefile Source: "/Rdata/SECR",
layer: "building" with 2492 features
It has 1 fields
library(secr)
#This is secr 3.2.1. For overview type ?secr
> qmask = make.mask(Quenda_traps,
+ buffer = 300,
+ type = "trapbuffer",
+ poly = fence,
+ poly.habitat = "FALSE")
Error in as.vector(data) : no method for coercing this S4 class to
a vector
dput(Quenda_traps)
structure(list(x = c(390576.21, 390637.85, 390594.93, 390528.49,
390646.58, 390488.12, 390681.01, 390499.98, 390632.29, 390677.26,
390642.7, 390710.33, 390690.37, 390741.81, 390588.01, 390655.06,
390575.97, 390246.66, 390340.13, 390236.33, 390309.59, 390295.93,
390164.11, 390065.71, 390120.42, 390117.17, 390091.7, 389875.57,
390179.69, 390157.45, 390164.94, 390151.02, 390172.17, 390246.28,
390263.25, 390256.32, 390308.2, 390135.06, 390093.3, 389914.13,
389916.76, 389869.37, 389809.17, 389782.5, 389818.78, 389802.75,
389818.78, 389771.52, 389792.74, 389791.25, 389905.36, 389832.62,
389886.16, 389863.21, 389908.68, 389912.46, 389902.05, 389528.11,
389661.6, 389689.54, 389657.88, 389678.71, 389569.25, 389618.44,
389564.87, 389615.37, 389662.18, 389630.96, 389713.09, 389654.91,
389744.37, 389762.02, 389715.87, 389696.2), y = c(6451727.44,
6451613.91, 6451566.89, 6451511.85, 6451416.66, 6451402.77, 6451287.32,
6451177.83, 6451164.84, 6451108.78, 6451188.57, 6450929.54, 6450855.04,
6450723.66, 6450716.47, 6450451.11, 6450343.83, 6451821.46, 6451645.08,
6451553.05, 6451588.21, 6451541.5, 6451509.03, 6451442.56, 6451358.89,
6451222.49, 6451221.11, 6451303.03, 6451115.63, 6450989, 6450994.62,
6450797.13, 6450761.88, 6450717.22, 6450719.62, 6450399.14, 6450403.03,
6450352.38, 6450197.83, 6451841.15, 6451684.86, 6451526.92, 6451419.83,
6451441.72, 6451316.83, 6451367.43, 6451316.83, 6451235.39, 6451105.9,
6450981.72, 6450992.93, 6450910.1, 6450935.07, 6450787.37, 6450685.86,
6450684.79, 6450600.42, 6451656.26, 6451534.65, 6451395.26, 6451267.42,
6451262.1, 6451257.59, 6451248.14, 6451138.91, 6451096.22, 6451132.2,
6450964.46, 6450964.24, 6450844.98, 6450864.8, 6450717.54, 6450620.58,
6450519.48)), class = c("traps", "data.frame"), row.names = c("1001",
"1002", "1003", "1004.1", "1004.2", "1005.1", "1005.2", "1006.1",
"1006.2", "1006.3", "1006.4", "1007", "1008", "1009", "1010",
"1011", "1012", "2001", "2002", "2003.1", "2003.2", "2003.3",
"2004", "2005", "2006", "2007.1", "2007.2", "2008.1", "2008.2",
"2009.1", "2009.2", "2010.1", "2010.2", "2011.1", "2011.2", "2012.1",
"2012.2", "2013", "2014", "3001", "3002", "3003", "3004.1", "3004.2",
"3005.1", "3005.2", "3005.3", "3006", "3007", "3008.1", "3008.2",
"3009.1", "3009.2", "3010", "3011.1", "3011.2", "3012", "4001",
"4002", "4003", "4004.1", "4004.2", "4005.1", "4005.2", "4006",
"4007.1", "4007.2", "4008.1", "4008.2", "4009.1", "4009.2", "4010",
"4011", "4012"), detector = "multi", usage = structure(c(1, 1,
1, 1, 0, 1, 0, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1,
1, 1, 1, 0, 1... ), .Dim = c(74L, 85L), .Dimnames = list(
c("1001", "1002", "1003", "1004.1", "1004.2", "1005.1", "1005.2",
"1006.1", "1006.2", "1006.3", "1006.4", "1007", "1008", "1009",
"1010", "1011", "1012", "2001", "2002", "2003.1", "2003.2",
"2003.3", "2004", "2005", "2006", "2007.1", "2007.2", "2008.1",
"2008.2", "2009.1", "2009.2", "2010.1", "2010.2", "2011.1",
"2011.2", "2012.1", "2012.2", "2013", "2014", "3001", "3002",
"3003", "3004.1", "3004.2", "3005.1", "3005.2", "3005.3",
"3006", "3007", "3008.1", "3008.2", "3009.1", "3009.2", "3010",
"3011.1", "3011.2", "3012", "4001", "4002", "4003", "4004.1",
"4004.2", "4005.1", "4005.2", "4006", "4007.1", "4007.2",
"4008.1", "4008.2", "4009.1", "4009.2", "4010", "4011", "4012"
), c("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11",
"12", "13", "14", "15", "16", "17", "18", "19", "20", "21",
"22", "23", "24", "25", "26", "27", "28", "29", "30", "31",
"32", "33", "34", "35", "36", "37", "38", "39", "40", "41",
"42", "43", "44", "45", "46", "47", "48", "49", "50", "51",
"52", "53", "54", "55", "56", "57", "58", "59", "60", "61",
"62", "63", "64", "65", "66", "67", "68", "69", "70", "71",
"72", "73", "74", "75", "76", "77", "78", "79", "80", "81",
"82", "83", "84", "85"))), spacex = 0.240000000048894, spacey = 0.21999999973923, spacing = 79.5867492192872)

Add background to ggplot2 plot based on third variable

I would like to add a background to the following plot, based on a third variables (variable p in the data below). However, I don't get to make it work. There are some previous related post like for instance this one. However, it does not seem to work in my case.
THE DATA
out121<-structure(list(MEp_R = c(-0.00798875313192999, -0.0148676123843391,
-0.00682061802389229, -0.00342320747900698, -0.0151338635205871,
-0.0107317027491587, -0.0251293011821259, -0.00442888716467913,
-0.00336413112985843, -0.00684252019402579, -0.0179441683421641,
-0.0183345869013467, -0.00648428720725909, -0.0214876248035767,
-0.016436676184706, -0.0374053532385283, -0.0149373698200334,
-0.00812201774657575, -0.00845365671158477, -0.00770113356603315,
-0.0235236091649431, -0.00319728180470263, -0.00375490467048723,
-0.0105497059284769, -0.0113949568001174, -0.00796452607813328,
-0.018162596356658, -0.0305161849884928, -0.0182695024428088,
-0.00733874764247203, -0.0258767466756409, -0.00800032458837883,
-0.0254092616701198, -0.00796500399596014, -0.018098828213091,
-0.00403853247796117, -0.00537848845812282, -0.0150529001843169,
-0.0184434922700636, -0.028401757025326, -0.00394247092784099,
-0.00389984372897549, -0.0212293138913338, -0.00323995037136412,
-0.00435171103575741, -0.0142661346880167, -0.00763574799777637,
-0.0243480163192293, -0.0334550531174489, -0.00398665257789114,
-0.0269010788141356, -0.0339462390051263, -0.00731023368759553,
-0.00273217917938973, -0.00721679095419613, -0.0284960447574926,
-0.0256689850381392, -0.00418816161208601, -0.0207469376148546,
-0.0159256736739084, -0.00313049343988148, -0.0280083118773398,
-0.00305634712789695, -0.00222991072333599, -0.00728714579423012,
-0.00807506060034409, -0.0152635924760586, -0.0332733629111364,
-0.00760293035610356, -0.0139815979579726, -0.00677123700835164,
-0.0215960340688805, -0.0338156227799011, -0.0253065053249637,
-0.018647649832361, -0.0354963987663701, -0.022253017915301,
-0.00671809517372551, -0.00732452244298519, -0.0319724784878375,
-0.0342471218743405, -0.0350620385653106, -0.0230236881980878,
-0.0346963784053919, -0.00282689342327851, -0.00325484604261175,
-0.033849668042083, -0.00413139898221564, -0.0228993329834933,
-0.00559908152238757, -0.0183537460976775, -0.00472047771208315,
-0.0251909804124543, -0.0311011436015412, -0.0229936133519116,
-0.00308542036103775, -0.0228912921993044, -0.012939181841082,
-0.0230877632656367, -0.032591064432755, -0.00735469284998116,
-0.0303916947589657, -0.00314093237962542, -0.0231490730231191,
-0.0054652608506885, -0.025241520518079, -0.00445136785085343,
-0.0128593850454578, -0.00673762742746708, -0.0226916858622337,
-0.0161806428228128, -0.0209040965403646, -0.020750711338677,
-0.0181057154876848, -0.0178080592822426, -0.0176328580881027,
-0.0176110802336996, -0.0176155620180982, -0.0178561500821362,
-0.0154770868242239, -0.0155229482583411, -0.0180674673481902,
-0.0178800497751207, -0.0154828650185023, -0.0154750397213182,
-0.0152368261892578, -0.0154977482766045, -0.0182346532616592,
-0.0183274537639035, -0.0168011783244315, -0.0168016019401702,
-0.0149946562681369, -0.0158317352920475, -0.0167520543181098,
-0.0166157299693747, -0.0158279779461525, -0.0149977762443961,
-0.0180750003460435, -0.0151529841212906, -0.017923432133514,
-0.017928916003054, -0.0179127560726451, -0.0180570721490242,
-0.0177899027947602, -0.0158531006505291, -0.0174593473238637,
-0.0151291673194569, -0.0179473924690324, -0.017693119507179,
-0.0177884007129345, -0.0148359633238028, -0.0178372767093443,
-0.0179573607500675, -0.0178478763689906, -0.0159047471769866,
-0.0179332447429962, -0.0148569280408044, -0.0179181675050371,
-0.0166326656412793, -0.0148692975727, -0.0159248001099628, -0.0181012191592349,
-0.0178451421528243, -0.0165514761611982, -0.0157213399905123,
-0.0157870112143077), MEp_Irr = c(0.0267063409682918, 0.0348657385506534,
0.0342691355007968, -0.115640975842031, 0.0387492569408993, 0.0381593243875236,
0.0427698849005908, -0.0173766138908777, -0.137295803791003,
0.0255837365606775, 0.033333234010886, 0.04219733855297, 0.0283972511296412,
0.042063245938688, 0.0381434891186468, 0.116843385842082, 0.0303337697359422,
0.0270649725588233, 0.0408800022550086, 0.0212509208656095, 0.0411728527295687,
-0.124549799025409, -0.0144478716604061, 0.0423901861335305,
0.0415985024073151, 0.037704775866327, 0.0422604508495823, 0.0763588031748094,
0.0426357961926886, 0.0329608493967286, 0.0496266735787044, 0.040016714764635,
0.0519859953665068, 0.0394245716448228, 0.0342680692030386, -0.0267193515702238,
-0.0177971867890813, 0.0381256479675564, 0.0355367325792796,
0.0727607935148329, -0.0326388375297887, -0.0178079477006394,
0.0410572010103166, -0.117374678254156, -0.020320609244612, 0.0370392597214022,
0.0353728091272744, 0.0449625606410676, 0.0753850352272533, -0.0343858665221561,
0.0698051617341533, 0.077951540991422, 0.0237442438741627, -0.058297571549435,
0.0243284890185037, 0.0772458962449727, 0.0543866044441634, -0.00428096536358291,
0.04038564508393, 0.0354424565177792, -0.131352692076439, 0.0720546321152786,
-0.0665837597449708, -0.127968519873726, 0.0330986089266455,
0.0259981472748394, 0.0335310580145865, 0.0796945930317802, 0.020663774953603,
0.0293980728446607, 0.0362259277862459, 0.0432046089419517, 0.0685995447124759,
0.0469963521357076, 0.0354789715561056, 0.0709742762976496, 0.0452791300187091,
0.0331219798716994, 0.0295434908925642, 0.08744851179291, 0.0799768054034634,
0.0815503850718988, 0.0440574836496075, 0.0839865621030131, -0.0688754800689186,
-0.116779517177347, 0.0694952520459083, -0.0264949177920162,
0.0413932242242241, 0.0172278601886947, 0.0420753996022112, -0.144255178922228,
0.0470894777847475, 0.076135949143235, 0.041744283972074, -0.0630340966211796,
0.0413829074872194, 0.0396332210621789, 0.041111622411323, 0.102474470476636,
0.0325757173791441, 0.0760653479136575, -0.123211363037203, 0.0410342604744772,
-0.0174831909563214, 0.0530941558728644, -0.0418843636393472,
0.038435679009942, 0.0336208563560449, 0.0399056440442261, 0.0265540871197283,
0.0444367528340963, 0.0435895606359242, 0.0215615450224718, 0.0209324963084151,
0.0202690410290722, 0.0194304147117461, 0.0197015376925802, 0.0232944851025464,
0.0194971575601002, 0.0195128222684842, 0.0229793934050297, 0.022601136427616,
0.020063571212835, 0.0199659939198503, 0.0189019086482273, 0.020421297722678,
0.0238829270946739, 0.0242153520276344, 0.0261331103214652, 0.0259380402516094,
0.0195777353030817, 0.0234436006049973, 0.0253584791696736, 0.025820695095831,
0.0242819014695358, 0.0195352848772439, 0.0287263051849994, 0.0195826041035442,
0.025491942513768, 0.0244647468662558, 0.0257233178691396, 0.0262291526865683,
0.0247672209152699, 0.0245726192470798, 0.0273106213867739, 0.0199937372665979,
0.027112767769544, 0.027267180444377, 0.0261432756569257, 0.0198848926943087,
0.0256279615107693, 0.0279085914888886, 0.0285149093762463, 0.0240689981341775,
0.0266901583531034, 0.0200700646179756, 0.0262578329932839, 0.0263488901038499,
0.0201551487864926, 0.0244667611324202, 0.0262710081046724, 0.0264923641418946,
0.0252091848629891, 0.0230331062657154, 0.0230721882488657),
p = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1.33226762955019e-15,
1.99840144432528e-15, 1.31006316905768e-14, 2.42694753183059e-13,
3.68407526707415e-10, 1.61860724823271e-08, 1.27863204824052e-06,
2.38962085785666e-06, 4.89028683459125e-06, 1.29586352866085e-05,
1.3194564602248e-05, 5.02157467259234e-05, 6.66173260168712e-05,
0.000452773851216115, 0.00058440281230121, 0.000733510241929469,
0.000880660946449119, 0.0136063339510712, 0.0151237168548795,
0.0353698786443037, 0.0615641433741911, 0.0627726749529298,
0.0763356770320813, 0.0824154857507584, 0.0929893345348154,
0.0947385432373617, 0.0958710673877281, 0.104039435798143,
0.117891950799349, 0.137872040413549, 0.173995516565528,
0.216611706295959, 0.277748137930015, 0.29066025684483, 0.294964122909925,
0.360934324888994, 0.390659280716865, 0.399257628135196,
0.415075121018395, 0.415906223140706, 0.429535524186567,
0.4346739056448, 0.461459259127042, 0.492938667179821, 0.552802494760443,
0.606427873500538, 0.608777827159917, 0.621809390011274,
0.65212833158726, 0.720281808796569, 0.725498755645982, 0.732029138010702,
0.873071745150822, 0.902596817430848, 0.965286951991386,
0.979065101832578)), datalabel = "Written by R. ", time.stamp = " 6 Aug 2017 22:23", .Names = c("MEp_R",
"MEp_Irr", "p"), formats = c("%9.0g", "%9.0g", "%9.0g"), types = c(255L,
255L, 255L), val.labels = c("", "", ""), var.labels = c("MEp_R",
"MEp_Irr", "p"), row.names = c("1", "2", "3", "4", "5", "6",
"7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17",
"18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28",
"29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39",
"40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "50",
"51", "52", "53", "54", "55", "56", "57", "58", "59", "60", "61",
"62", "63", "64", "65", "66", "67", "68", "69", "70", "71", "72",
"73", "74", "75", "76", "77", "78", "79", "80", "81", "82", "83",
"84", "85", "86", "87", "88", "89", "90", "91", "92", "93", "94",
"95", "96", "97", "98", "99", "100", "101", "102", "103", "104",
"105", "106", "107", "108", "109", "110", "111", "112", "113",
"114", "115", "116", "117", "118", "119", "120", "121", "122",
"123", "124", "125", "126", "127", "128", "129", "130", "131",
"132", "133", "134", "135", "136", "137", "138", "139", "140",
"141", "142", "143", "144", "145", "146", "147", "148", "149",
"150", "151", "152", "153", "154", "155", "156", "157", "158",
"159", "160", "161", "162", "163", "164", "165", "166"), version = 12L, class = "data.frame")
THE PLOT
out121$perc1<-c(1:166)
l64<-ggplot(out121,aes(perc1))
l65<-l64+geom_line(aes(y=MEp_R,colour="var1"),size=1.3)+
geom_line(aes(y=MEp_Irr,colour="var2"),size=1.3)+
scale_colour_manual(values=c("blue3","green"), name="MEt")+
scale_x_discrete(name="Threshold (in percentage)",breaks=c(0, 250, 500,750,1000),
labels=c("0", "25", "50","75","100")) +
scale_y_continuous(name="MEp",limits = c(-0.7, 0.5),breaks=c(-0.3,-0.1,0,0.1,0.3,0.5))
l66<-l65+ theme_bw()+ggtitle("Full sample ") +
theme(plot.title = element_text(lineheight=.8, face="bold"),legend.position="bottom")+
guides(col=guide_legend(ncol=2,title.position="top"))+
theme(panel.background = element_rect(fill = NA),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank())
THE QUESTION
I now would like to add a background to this plot, based on variable "p".
The variable only has a few values bigger than 0.05. So only for those points the background should be coloured.
That means, points lower than 0.05 should have a white background, the other points should have a red (for instance) background.
I made some unfortunate efforts based on the following code but I don't get the scaling (white/blue right, not even on a continuous basis) and I don't get the color in the background. Could somebody help me please? Thank you very much!
geom_rect(aes(xmin = 0, xmax = 1000, ymin = -Inf, ymax = Inf,fill = p))+
scale_color_gradient2(midpoint=0.05, low="white", mid="light blue",high="blue", space ="Lab" )
One of the errors I for instance get is:
"Scale for 'colour' is already
present. Adding another scale for
'colour', which will replace the
existing scale." But even if that is solved, it doesn't solve the fact that I only have one color on my background graph.
First
out121$p_above <- out121$p > 0.05
and then
l66 + geom_rect(aes(xmin = perc1 - 0.5, xmax = perc1 + 0.5,
ymin = -Inf, ymax = Inf, fill = p_above), alpha = 0.4) +
scale_fill_manual(values = c("white", "red"))

combine two data frames into one while keeping the original row numbers

I'm struggling with a very simple problem. I have two data frames to be combined into a single data frame while retaining their original row names. If you're interested to play with the two data frames:
> dput(cc)
structure(list(c = c(166.081273211195, 117.874627144804, 85.7050194973198,
122.960746859139, 144.149802403233, 90.8034500957001, 89.5265981283352,
77.8535718910714, 144.544005656701, 115.597165020403, 109.028391182666,
89.4045716355402, 77.7944830105746, 69.3378920684953, 79.9094499459695,
146.768077595585, 157.933946809176, 92.562729606313, 62.5081476457419,
90.1081848285295, 111.830482369239, 111.596975757741, 99.2311075024839,
145.204385556523, 99.0215341758211, 75.4302512245677, 92.0094563363458,
77.7314808830408)), .Names = "c", row.names = c("26", "27", "28",
"29", "35", "36", "37", "38", "39", "40", "46", "47", "48", "49",
"50", "51", "52", "56", "57", "58", "59", "60", "61", "62", "69",
"70", "71", "72"), class = "data.frame")
> dput(ccc)
structure(list(b = c(76.376257255471, 61.8314936138378, 62.769450181685,
73.6356164203567, 111.690756826382, 76.9294523843767, 61.3534699857719,
69.3647221333577, 83.9764878084258, 81.3800252294203, 69.5091780233591,
87.3595961209547, 78.5074999563006, 74.4479256924594, 81.5920316281566,
96.3417259554163, 75.4138056616399, 76.0553034201146, 95.1759950844736,
81.3252467041995, 86.306305649635, 70.5626459312969, 72.7797520793756,
119.49702877934, 123.268678343102, 88.0450051118928, 76.2139948860248,
98.1496728839206, 126.396927030103, 146.058540478643, 115.7341525964,
87.280600158726, 78.0274068331766, 122.817977752389, 142.491559175427,
152.895839114334, 94.4932174696818, 117.167042165763, 85.5340971715004,
101.480170738897, 117.759691799033, 128.998051359269, 98.3180491401911,
84.5915489017958, 87.4927520958843, 75.5366495973031, 118.088343275321,
121.375320935357, 94.7724147096235, 70.2266610201599, 123.158462686523,
76.87408931845, 94.4365460662552, 105.952134808703, 76.821070196668
)), .Names = "b", row.names = c("1", "2", "3", "4", "5", "6",
"7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17",
"18", "19", "20", "21", "22", "23", "24", "25", "30", "31", "32",
"33", "34", "41", "42", "43", "44", "45", "53", "54", "55", "63",
"64", "65", "66", "67", "68", "73", "74", "75", "76", "77", "78",
"79", "80", "81", "82", "83"), class = "data.frame")
The easiest way to combine is
> c(cc$c, ccc$b)
[1] 166.08127 117.87463 85.70502 122.96075 144.14980 90.80345 89.52660 77.85357 144.54401
[10] 115.59717 109.02839 89.40457 77.79448 69.33789 79.90945 146.76808 157.93395 92.56273
[19] 62.50815 90.10818 111.83048 111.59698 99.23111 145.20439 99.02153 75.43025 92.00946
[28] 77.73148 76.37626 61.83149 62.76945 73.63562 111.69076 76.92945 61.35347 69.36472
[37] 83.97649 81.38003 69.50918 87.35960 78.50750 74.44793 81.59203 96.34173 75.41381
[46] 76.05530 95.17600 81.32525 86.30631 70.56265 72.77975 119.49703 123.26868 88.04501
[55] 76.21399 98.14967 126.39693 146.05854 115.73415 87.28060 78.02741 122.81798 142.49156
[64] 152.89584 94.49322 117.16704 85.53410 101.48017 117.75969 128.99805 98.31805 84.59155
[73] 87.49275 75.53665 118.08834 121.37532 94.77241 70.22666 123.15846 76.87409 94.43655
[82] 105.95213 76.82107
But doing this recreate the row numbers. Is there any simple function to combine while keeping the row names intact? Thanks!
These lines of code should solve the problem
z0 <- as.numeric(c(rownames(cc), rownames(ccc)))
z <- data.frame(c(cc$c, ccc$b))
row.names(z) <- z0
data.frame(z[order(as.numeric(row.names(z))),])

Computing angle between two vectors (with one vector having a specific X,Y position)

I am trying to compute the angle between two vectors, wherein one vector is fixed and the other vector is constantly moving. I already know the math in this and I found a code before:
theta <- acos( sum(a*b) / ( sqrt(sum(a * a)) * sqrt(sum(b * b)) ) )
I tried defining my a as:
a<-c(503,391)
and my b as:
b <- NM[, c("X","Y")]
When I apply the theta function I get:
Warning message:
In acos(sum(a * b)/(sqrt(sum(a * a)) * sqrt(sum(b * b)))) : NaNs produced
I would appreciate help to solve this.
And here is my sample data:
structure(list(A = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label =
c("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12",
"13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23",
"24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34",
"35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45",
"46", "47", "48", "49", "50", "51", "52", "53", "54", "55", "56",
"57", "58", "59", "60", "61", "62", "63", "64", "65", "66", "67",
"68", "69", "70", "71", "72", "73", "74", "75", "76", "77", "78",
"79", "80", "81", "82", "83", "84", "85", "86", "87", "88", "89",
"90", "91", "92", "93", "94", "95", "96", "97", "98", "99", "100",
"101", "102", "103", "104", "105", "106", "107", "108", "109",
"110"), class = "factor"), T = c(0.1, 0.2, 0.3, 0.4, 0.5, 0.6 ), X =
c(528.04, 528.04, 528.04, 528.04, 528.04, 528.04), Y = c(10.32,
10.32, 10.32, 10.32, 10.32, 10.32), V = c(0, 0, 0, 0, 0, 0),
GD = c(0, 0, 0, 0, 0, 0), ND = c(NA, 0, 0, 0, 0, 0), ND2 = c(NA,
0, 0, 0, 0, 0), TID = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = c("t1",
"t10", "t100", "t101", "t102", "t103", "t104", "t105", "t106",
"t107", "t108", "t109", "t11", "t110", "t12", "t13", "t14",
"t15", "t16", "t17", "t18", "t19", "t2", "t20", "t21", "t22",
"t23", "t24", "t25", "t26", "t27", "t28", "t29", "t3", "t30",
"t31", "t32", "t33", "t34", "t35", "t36", "t37", "t38", "t39",
"t4", "t40", "t41", "t42", "t43", "t44", "t45", "t46", "t47",
"t48", "t49", "t5", "t50", "t51", "t52", "t53", "t54", "t55",
"t56", "t57", "t58", "t59", "t6", "t60", "t61", "t62", "t63",
"t64", "t65", "t66", "t67", "t68", "t69", "t7", "t70", "t71",
"t72", "t73", "t74", "t75", "t76", "t77", "t78", "t79", "t8",
"t80", "t81", "t82", "t83", "t84", "t85", "t86", "t87", "t88",
"t89", "t9", "t90", "t91", "t92", "t93", "t94", "t95", "t96",
"t97", "t98", "t99"), class = "factor")), .Names = c("A", "T", "X", "Y", "V", "GD", "ND", "ND2", "TID"), row.names = c(NA, 6L),
class = "data.frame")
Your function is not vectorized. Try this:
theta <- function(x,Y) apply(Y,1,function(y,x) acos( sum(x*y) / ( sqrt(sum(x^2)) * sqrt(sum(y^2)) ) ),x=x)
a<-c(503,391)
b <- DF[, c("X","Y")]
theta(a,b)
# 1 2 3 4 5 6
#0.6412264 0.6412264 0.6412264 0.6412264 0.6412264 0.6412264
There is a problem with the acos and atan functions in this application, as you cannot compute angles for the full circle, only for the plus quadrant. In 2D, you need two values to specify a vector, and you need two values (sin and cos) to define it in degrees/radians up to 2pi. Here is an example of the acos problem:
plot(seq(1,10,pi/20)) ## A sequence of numbers
plot(cos(seq(1,10,pi/20))) ## Their cosines
plot(acos(cos(seq(1,10,pi/20)))) ## NOT Back to the original sequence
Here's an idea:
angle <- circular::coord2rad(x, y)
plot(angle)
where "(x,y)" has "angle"
as.numeric(angle)
gives the angle in radians (0,360). To report geographical directions, convert to degrees, and other things, you can use the added parameters for the circular function, e.g.:
x <- coord2rad(ea,eo, control.circular = list(type = "directions",units = "degrees"))
plot(x)
as.numeric(x)

Fit gamma mixture to fertility schedule in R

I am trying to fit a gamma mixture model (two gamma distributions) to an age-fertility profile. I have a dataset containing age specific fertility rates and age, and I want to fit two gammas in order to find the corresponding parameters (in the end I will use fertility profiles from different years and try to see how the parameters evolve over time). I have so far tried to use mixtools library (gammamixEM) but without success. I would be very grateful for some help.
Ale
a<- structure(list(EDAD = structure(1:45, .Label = c("11", "12", "13", "14", "15",
"16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29",
"30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43",
"44", "45", "46", "47", "48", "49", "50", "51", "52", "53", "54", "55", "Total" ),
class = "factor"), value = c(0, 0, 0, 0, 0.002761668, 0.006712018, 0.010820244,
0.017867778, 0.029533765, 0.034055242, 0.036665669, 0.043840421, 0.042949584,
0.042344612, 0.050588917, 0.050187588, 0.054114728, 0.057258792, 0.059280324,
0.062566731, 0.062369629, 0.062154767, 0.063734337, 0.058236776, 0.052623842,
0.046330921, 0.040639027, 0.033707865, 0.02531141, 0.017651534, 0.010953808,
0.007463863, 0.003224766, 0.002190101, 0.001117443, 0.000465116, 0.000363901,
0.00012647, 0.000267326, 0.000280308, 0, 0, 0, 0, 0)), .Names = c("EDAD", "value"),
class = "data.frame", row.names = 79596:79640)
The reason why it won't run is because you have zeroes in your data set. Here is what you could do:
aa <- a$value[a$value > 0]
Now you can fit the gamma mixture
require(mixtools)
g3 <- gammamixEM(aa)
Now check that it looks OK by plotting the fitted mixture density.
d3 <- function(x) g3$lambda[1]*dgamma(x, g3$gamma.pars[1], 1/g3$gamma.pars[2]) + g3$lambda[2]*dgamma(x, g3$gamma.pars[3], 1/g3$gamma.pars[4])
Here is another pitfall: gammamixEM apparently parametrises the gamma distribution differently to R. Why? Who knows?
x <- seq(min(aa), max(aa), 0.001)
plot(x, d3(x), "l")
hist(aa, col="pink", add=T, freq=F, breaks=10)
Looks reasonable, if far from perfect.

Resources