Select same name object with two dimension - r

Below is the sample data. May I ask how can I select all ge_v at once? If there is only single number inside [], I know I can use sprintf ('ge_v[%d]', seq(10)) to select ge_v[1]...ge_v[10]. How can I do the same thing for two dimensional data?
structure(list(rn = c("ge_v[27,1]", "ge_v[28,2]", "ge_v[29,3]",
"ge_v[30,4]", "ge_v[31,5]", "ge_v[32,6]", "ge_v[33,7]", "ge_v[34,8]",
"ge_v[35,9]", "ge_v[36,10]", "ge_v[37,11]"), mean = c(0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0)), row.names = c("70", "71", "72", "73",
"74", "75", "76", "77", "78", "79", "80"), class = "data.frame")

Perhaps this helps
sprintf ('ge_v[%d,%d]', 27:36, 1:10)

Related

Match rownames and colnames of two dataframes

I have two dataframes. The row names for one of them (metadata) is the same as the column names for the second (TPM).
I reordered the rownames for metadata in a certain way, now I want to reorder the colnames for TPM accordingly. Both of them should be the same order.
First, I make sure that the rownames in metadata are really the same as the colnames in TPM, any uncommon row or column between the two, will be deleted:
INDEX = intersect(colnames(TPM),rownames(metadata))
metadata = metadata[INDEX,]
TPM = TPM[,INDEX]
Then, I make the reorderring of the metadata rownames (it's according to a feature in the metadata, doesn't really matter):
metadata = metadata %>% arrange(Benefit)
And now comes the problem, I want the TPM colnames order to be the same as the metadata rownames. I want to change the TPM colnames order, I tired this:
index = order(colnames(TPM),rownames(metadata))
TPM = TPM[,index]
but TPM didn't change at all, I also tried this code with match and intersect. Still TPM didn't change at all. I don't want to change metadata's rownames, it's already in the order I want.
Why TPM's colnames aren't changing?
Sample of TPM:
structure(list(Pt1 = c(12.1089467388298, 0, 18.0385362276576,
1.92790576596844, 94.2409551672849, 16.8882703013677, 23.1934795882213
), Pt10 = c(5.31468107381049, 0, 19.5665754959778, 6.08224068432115,
188.508358461147, 8.48446380342082, 9.79919096042849), Pt8 = c(9.07821549589067,
0, 14.5403817716173, 9.291006716028, 101.817341286849, 12.4642830001982,
23.666833737613), Pt101 = c(6.62551158552007, 0, 44.4975176830514,
5.6097199560158, 29.3442077622205, 4.47109939839761, 22.5645567722583
), Pt103 = c(18.1736419550473, 0, 19.8694720219646, 0.385296099264051,
10.7494896282499, 5.30680045140835, 6.25389746004431), Pt106 = c(7.28900047884896,
0.25821427874803, 23.1149486669295, 80.936069669191, 117.783929643365,
10.167975612355, 23.7821668347939), Pt11 = c(2.81580497213944,
0, 12.8578712768363, 1.8638846278409, 66.1843554522191, 12.0730665529163,
18.8979998503006)), row.names = c("A1BG", "NAT2", "ADA", "CDH2",
"AKT3", "ZBTB11-AS1", "MED6"), class = "data.frame")
Sample of metadata:
structure(list(Cohort = c("NIV3-PROG", "NIV3-NAIVE", "NIV3-NAIVE",
"NIV3-PROG", "NIV3-PROG", "NIV3-NAIVE", "NIV3-PROG"), Response = c("PD",
"SD", "PD", "PD", "PD", "PD", "SD"), `Dead/Alive
(Dead = True)` = c(TRUE,
TRUE, TRUE, FALSE, TRUE, TRUE, TRUE), `Time to Death
(weeks)` = c(22.85714286,
36.57142857, 37, 69.14285714, 13, 119.5714286, 61.28571429),
Subtype = c("CUTANEOUS", "CUTANEOUS", "MUCOSAL", "CUTANEOUS",
"MUCOSAL", "CUTANEOUS", "OCULAR/UVEAL"), `Mutational
Subtype` = c("NA",
"NF1", "TripleWt", "TripleWt", "BRAF", "BRAF", "TripleWt"
), `M Stage` = c("M1C", "M1A", "M0", "M1B", "M1C", "NA",
"M1C"), `Mutation Load` = c("NA", "75", "87", "21", "700",
"106", "33"), `Neo-antigen Load` = c("NA", "33", "44", "5",
"219", "67", "14"), `Neo-peptide Load` = c("NA", "56", "69",
"11", "273", "187", "25"), `Cytolytic Score` = c("977.86911190000001",
"65.840716889999996", "457.43633440000002", "1108.8620289999999",
"645.54163300000005", "602.6740413", "228.61321050000001"
), Benefit = c("NoResponse", "NoResponse", "NoResponse",
"NoResponse", "NoResponse", "NoResponse", "NoResponse")), row.names = c("Pt1",
"Pt10", "Pt8", "Pt103", "Pt106", "Pt11", "Pt82"), class = "data.frame")
I think you can just sort the INDEX vector:
INDEX=sort(intersect(colnames(TPM),rownames(metadata)))
metadata=metadata[INDEX,]
TPM=TPM[,INDEX]

How to perform many Mann-Whitney tests conditionally based on the value within a column in R?

I want to create a function (or a way to perform the test in one-go) that does Mann Whitney test. I want to analyze logSG values between 2 different CC conditions within the same Time. So for following dataframe, I want 3 p-values that correspond to each Time.
My sample dataframe:
structure(list(Time = c("30", "30", "30", "30", "30", "30", "30",
"30", "30", "30", "30", "30", "30", "60", "60", "60", "60", "60",
"60", "60", "60", "60", "60", "90", "90", "90", "90", "90", "90",
"90", "90", "90"), CC = c("Scramble", "Scramble", "Scramble",
"Scramble", "Scramble", "Scramble", "Scramble", "Scramble", "KD",
"KD", "KD", "KD", "KD", "Scramble", "Scramble", "Scramble", "Scramble",
"Scramble", "KD", "KD", "KD", "KD", "KD", "Scramble", "Scramble",
"Scramble", "Scramble", "KD", "KD", "KD", "KD", "KD"), logSG = c(0,
6.29469069760774, 6.97548510669835, 0, 0, 5.6529880324294, 0,
0, 0, 0, 0, 5.84818081635987, 0, 6.33960454566506, 0.410736902037262,
0, 0, 0, 0, 0.0294484401648161, 0, 1.03061195077248, -1.30321174424293,
-1.25902114646857, 0, 0, 0.787059500696643, 3.54611686297603,
0, 0, -0.297732408305282, 0)), row.names = c(NA, -32L), class = c("data.table",
"data.frame"), .internal.selfref = <pointer: 0x7f9b120204e0>)
I tried following for every time point:
e <- result[result$Time == 30,]
wilcox.test(SG ~ CC, data=e)
This is clunky and inefficient.
Or, I'm having trouble getting this to work:
t <- result %>% group_by(Time) %>% do(te=wilcox.test(logSG ~ CC))
If possible, I'd like to learn how to do this using both dplyr and m/s/apply.
References: link Link
If we are using do, then specify the data
library(dplyr)
result %>%
group_by(Time) %>%
do(te=wilcox.test(logSG ~ CC, data = .))
Or using map on nested dataset
library(purrr)
result %>%
group_by(Time) %>%
nest %>%
mutate(te = map(data, ~ wilcox.test(logSG ~ CC, data = .x) ))

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"))

How to delete a single cell?

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.

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)

Resources