I am trying to plot a time series plot using ggplot. I have a discrete variable that just describes if a vehicle engine was on or off through a given time.
Naturally, it should either be 0 or 1, there can't be an overlap. But as seen in the below plot, there's a considerable overlap.
I am pretty new to ggplot. Could someone please let me know what the issue is.
The code I am using is:
pd %>%
ggplot(aes(x = SampleTime, y = as.factor(Value))) + geom_line(color = 'black') +
labs(x = 'Time', y = 'Ignition Status', title = 'Ignition Status by Time') +
scale_x_datetime(date_breaks = '1 hour', date_labels = '%H') +
theme(axis.title = element_text(size = 18, face = 'bold'),
axis.text = element_text(size = 14, color = 'black'))
structure of data:
str(pd)
'data.frame': 474 obs. of 2 variables:
$ SampleTime: POSIXct, format: "2020-11-30 00:17:46" "2020-11-30 01:17:47" "2020-11-30 02:17:48" "2020-11-30 03:17:49" ...
$ Value : num 0 0 0 0 0 0 0 0 0 1 ...
data used:
structure(list(SampleTime = structure(c(1606675666, 1606679267,
1606682868, 1606686469, 1606690070, 1606693671, 1606697271, 1606700872,
1606704473, 1606706974, 1606707066, 1606707129, 1606707192, 1606707255,
1606707318, 1606707382, 1606707445, 1606707508, 1606707571, 1606707631,
1606707694, 1606707754, 1606707817, 1606707880, 1606707943, 1606708006,
1606708069, 1606708132, 1606708195, 1606708258, 1606708318, 1606708381,
1606708441, 1606708504, 1606708567, 1606708630, 1606708691, 1606708751,
1606708814, 1606708877, 1606708940, 1606709003, 1606709066, 1606709129,
1606709192, 1606709255, 1606709318, 1606709381, 1606709444, 1606709507,
1606709570, 1606709633, 1606709696, 1606709759, 1606709822, 1606709885,
1606709948, 1606710011, 1606710074, 1606710137, 1606710200, 1606710263,
1606710348, 1606710411, 1606710474, 1606710537, 1606710600, 1606710663,
1606710726, 1606710789, 1606710852, 1606710915, 1606710978, 1606711041,
1606711104, 1606711167, 1606711230, 1606711293, 1606711356, 1606711419,
1606711482, 1606711545, 1606711608, 1606711671, 1606711734, 1606711797,
1606711860, 1606711923, 1606711986, 1606712049, 1606712112, 1606712175,
1606712238, 1606712301, 1606712365, 1606712428, 1606712491, 1606712553,
1606712617, 1606712679, 1606712756, 1606712909, 1606712970, 1606713007,
1606713034, 1606713094, 1606713157, 1606713220, 1606713283, 1606713346,
1606713409, 1606713472, 1606713535, 1606713598, 1606713661, 1606713724,
1606713787, 1606713850, 1606713913, 1606713976, 1606714039, 1606714102,
1606714165, 1606714228, 1606714291, 1606714354, 1606714417, 1606714480,
1606714543, 1606714606, 1606714669, 1606714732, 1606714795, 1606714858,
1606714921, 1606714984, 1606715047, 1606715110, 1606715173, 1606715236,
1606715299, 1606715362, 1606715425, 1606715488, 1606715551, 1606715614,
1606715677, 1606715740, 1606715803, 1606715866, 1606715929, 1606715992,
1606716055, 1606716118, 1606716181, 1606716244, 1606716307, 1606716370,
1606716433, 1606716464, 1606716826, 1606716886, 1606716949, 1606717012,
1606717075, 1606717138, 1606717201, 1606717264, 1606717327, 1606717390,
1606717453, 1606717516, 1606717579, 1606717642, 1606717705, 1606717768,
1606717831, 1606717894, 1606717957, 1606718021, 1606718084, 1606718147,
1606718210, 1606718273, 1606718336, 1606718399, 1606718462, 1606718525,
1606718588, 1606718651, 1606718714, 1606718777, 1606718839, 1606718903,
1606718965, 1606719029, 1606719091, 1606719155, 1606719218, 1606719281,
1606719344, 1606719407, 1606719470, 1606719533, 1606719596, 1606719659,
1606719722, 1606719785, 1606719848, 1606719911, 1606719974, 1606720037,
1606720100, 1606720163, 1606720226, 1606720289, 1606720352, 1606720415,
1606720478, 1606720498, 1606721559, 1606721619, 1606721682, 1606721745,
1606721808, 1606721871, 1606721934, 1606721997, 1606722060, 1606722123,
1606722186, 1606722249, 1606722312, 1606722375, 1606722438, 1606722501,
1606722564, 1606722627, 1606722690, 1606722753, 1606722816, 1606722858,
1606722892, 1606722952, 1606723015, 1606723078, 1606723141, 1606723204,
1606723267, 1606723330, 1606723393, 1606723456, 1606723519, 1606723582,
1606723645, 1606723708, 1606723771, 1606723834, 1606723897, 1606723960,
1606724023, 1606724086, 1606724149, 1606724212, 1606724275, 1606724338,
1606724401, 1606724464, 1606724527, 1606724590, 1606724653, 1606724716,
1606724779, 1606724842, 1606724905, 1606724968, 1606725031, 1606725094,
1606725157, 1606725220, 1606725283, 1606725346, 1606725409, 1606725472,
1606725535, 1606725598, 1606725661, 1606725721, 1606725784, 1606725840,
1606729441, 1606730753, 1606730814, 1606730857, 1606731021, 1606731081,
1606731144, 1606731207, 1606731270, 1606731333, 1606731396, 1606731459,
1606731522, 1606731585, 1606731648, 1606731711, 1606731774, 1606731837,
1606731900, 1606731963, 1606732026, 1606732089, 1606732138, 1606732956,
1606733017, 1606733080, 1606733143, 1606733206, 1606733269, 1606733332,
1606733395, 1606733458, 1606733521, 1606733584, 1606733647, 1606733710,
1606733773, 1606733836, 1606733899, 1606733962, 1606734025, 1606734088,
1606734151, 1606734203, 1606735128, 1606735189, 1606735252, 1606735315,
1606735378, 1606735441, 1606735504, 1606735567, 1606735630, 1606735683,
1606739285, 1606742885, 1606744969, 1606745030, 1606745093, 1606745156,
1606745219, 1606745282, 1606745345, 1606745408, 1606745471, 1606745534,
1606745597, 1606745660, 1606745723, 1606745786, 1606745849, 1606745912,
1606745975, 1606746038, 1606746101, 1606746164, 1606746370, 1606746433,
1606746496, 1606746559, 1606746702, 1606746736, 1606746765, 1606746828,
1606746891, 1606746954, 1606747017, 1606747077, 1606747140, 1606747203,
1606747266, 1606747329, 1606747392, 1606747455, 1606747518, 1606747581,
1606747644, 1606747707, 1606747770, 1606747833, 1606747896, 1606747959,
1606748022, 1606748085, 1606748145, 1606748206, 1606748269, 1606748332,
1606748395, 1606748458, 1606748521, 1606748584, 1606748647, 1606748707,
1606748770, 1606748833, 1606748896, 1606748959, 1606749022, 1606749085,
1606749148, 1606749211, 1606749274, 1606749337, 1606749400, 1606749463,
1606749526, 1606749589, 1606749652, 1606749715, 1606749778, 1606749841,
1606749904, 1606749967, 1606750030, 1606750093, 1606750156, 1606750219,
1606750282, 1606750345, 1606750408, 1606750471, 1606750534, 1606750597,
1606750657, 1606750720, 1606750781, 1606750841, 1606750904, 1606750967,
1606751030, 1606751084, 1606754686, 1606758287, 1606759153, 1606759213,
1606759276, 1606759339, 1606759402, 1606759465, 1606759528, 1606759592,
1606759654, 1606759718, 1606759780, 1606759843, 1606759906, 1606759969,
1606760032, 1606760096, 1606760159, 1606760222, 1606760285, 1606760348,
1606760411, 1606760474, 1606760537, 1606760600, 1606760663, 1606760726,
1606760788, 1606760851, 1606760914, 1606760977), class = c("POSIXct",
"POSIXt"), tzone = ""), Value = c(0, 0, 0, 0, 0, 0, 0, 0, 0,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0,
0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1)), row.names = c(NA, -474L), class = "data.frame")
ggplot automatically connects the gaps between observations if you use geom_line. Try to use geom_point instead to see the gaps:
pd %>%
ggplot(aes(x = SampleTime, y = as.factor(Value))) +
geom_point(color = 'black') +
labs(x = 'Time', y = 'Ignition Status', title = 'Ignition Status by Time') +
scale_x_datetime(date_breaks = '1 hour', date_labels = '%H') +
theme(axis.title = element_text(size = 18, face = 'bold'),
axis.text = element_text(size = 14, color = 'black'))
Alternatively, you can use geom_line but don't convert Value to a factor and use the numeric value instead. This way you can see the change from 0 to 1 more clearly:
pd %>%
ggplot(aes(x = SampleTime, y = Value)) +
geom_line(color = 'black') +
labs(x = 'Time', y = 'Ignition Status', title = 'Ignition Status by Time') +
scale_x_datetime(date_breaks = '1 hour', date_labels = '%H') +
theme(axis.title = element_text(size = 18, face = 'bold'),
axis.text = element_text(size = 14, color = 'black'))
Lastly, a slightly more appropriate option for categorical data might be to use geom_step instead, which at least connects the two categories with a vertical line:
pd %>%
ggplot(aes(x = SampleTime, y = Value)) +
geom_step(color = 'black') +
labs(x = 'Time', y = 'Ignition Status', title = 'Ignition Status by Time') +
scale_x_datetime(date_breaks = '1 hour', date_labels = '%H') +
theme(axis.title = element_text(size = 18, face = 'bold'),
axis.text = element_text(size = 14, color = 'black'))
If that doesn't suffice, check out this post on how to connect the lines between each consecutive dot per group via geom_segment.
I have a df like this;
>df[1:6,1:6]
> ABT.263 ABT.869 ABT.888 AC220 Afatinib Afatinib.rescreen.
> BT-20 1 1 1 1 0 1
> CAL-120 0 1 1 1 1 1
> CAL-51 1 1 1 1 1 1
> CAL-85-1 1 1 1 1 1 0
> DU-4475 0 1 1 1 1 1
> HCC1143 1 1 1 1 0 1
Basically, I want to extract 1's and 0's of each column's row name. Then put them into adjacent columns in a loop form.
Desired output is like this;
> final_df[1:6,1:4]
> ABT.263_1 ABT.263_0 ABT.869_1 ABT.869_0
> BT-20 CAL-120 BT-20 NA
> CAL-51 DU-4475 CAL-120
> CAL-85-1 CAL-51
> HCC1143 CAL-85-1
> DU-4475
> HCC1143
I can extract rownames of 0 cells and 1 cells separately with this;
value1 <- rownames(df)[which(df[,1] == 1)]
value0 <- rownames(df)[which(df[,1] == 0)]
However, failed to assign value1 and value0 vectors respectively in a df format with loops.
> dput(head(resp_tnbc, 20))
structure(list(ABT.263 = c(1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 0, 1,
1, 1, 1, 1, 1), ABT.869 = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1), ABT.888 = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1), AC220 = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 0,
1, 1, 1, 1, 1, 1, 1), Afatinib = c(0, 1, 1, 1, 1, 0, 1, 1, 1,
0, 1, 0, 1, 1, 1, 0, 1), Afatinib.rescreen. = c(1, 1, 1, 0, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1), AG.014699 = c(1, 1, 0, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), AICAR = c(1, 1, 1, 1,
1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0), AKTinhibitorVIII = c(1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1), AMG.706 = c(1,
1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1), AP.24534 = c(1,
1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), AR.42 = c(1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), AS601245 = c(1,
1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), AS605240 = c(1,
1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), AT.7519 = c(1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), ATRA = c(1,
1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1), AUY922 = c(1,
1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), AV.951 = c(1,
1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1), Axitinib = c(1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0), AZD6244 = c(1,
1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1), AZD6244.1 = c(1,
1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), AZD6482 = c(0,
1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1), AZD6482.1 = c(0,
1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1), AZD7762 = c(1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), AZD8055 = c(1,
1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1), BAY61.3606 = c(1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), Bexarotene = c(1,
1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), Bicalutamide = c(1,
1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), Bicalutamide.1 = c(1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), BIRB0796 = c(1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0), BIX02189 = c(1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), Bleomycin = c(1,
1, 0, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1), Bleomycin.50uM. = c(1,
1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), BMN.673 = c(1,
1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), BMS.345541 = c(1,
1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), BMS.536924.1 = c(1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), BMS.708163 = c(1,
0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1), BMS.708163.1 = c(1,
1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1), BMS.754807 = c(1,
1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), Bosutinib = c(1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), Bryostatin1 = c(1,
1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1), BX.795 = c(1,
1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), BX.912 = c(1,
1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1), CAL.101 = c(1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1), Camptothecin = c(1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), CAY10603 = c(1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), CCT007093 = c(1,
1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), CCT018159 = c(1,
1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1), CEP.701 = c(1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), Cetuximab = c(0,
1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1), CH5424802 = c(1,
1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 1), CHIR.99021 = c(1,
1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1), CHIR.99021.1 = c(1,
1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), CI.1040 = c(1,
1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), Cisplatin = c(1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), CP466722 = c(1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), CP724714 = c(1,
1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), CUDC.101 = c(1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), CX.5461 = c(1,
0, 1, 0, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 1), Cytarabine = c(1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), Dabrafenib = c(1,
0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), DMOG = c(1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), Docetaxel = c(1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), Doxorubicin = c(1,
1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), EHT1864 = c(1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), EKB.569 = c(1,
1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1), Elesclomol = c(1,
1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1), Embelin = c(1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), EpothiloneB = c(1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), Etoposide = c(1,
1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), EX.527 = c(1,
1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1), FH535 = c(1,
1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1), FK866 = c(1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), FMK = c(1, 1,
1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), FR.180204 = c(1,
1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), FTI.277 = c(1,
1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), GDC0941 = c(1,
1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1), GDC0941.rescreen. = c(1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), Gefitinib = c(1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1), Gemcitabine = c(1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), GenentechCpd10 = c(1,
1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), GSK.1904529A = c(1,
1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 1), GSK.650394 = c(1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), GSK1070916 = c(1,
1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1), GSK2126458 = c(1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), GSK269962A.1 = c(1,
0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0), GSK429286A = c(1,
1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), GSK690693 = c(0,
1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 1, 1, 1, 1, 0, 0), GW.2580 = c(1,
1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1), GW441756 = c(1,
1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0),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001.270 = c(1,
1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), KU.55933 = c(1,
1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1), LAQ824 = c(1,
1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), Lenalidomide = c(1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), LFM.A13 = c(1,
1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1), LY317615 = c(1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), Masitinib = c(1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), Methotrexate = c(1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), Midostaurin = c(1,
1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), MitomycinC = c(1,
1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), MK.2206 = c(1,
1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0), MLN4924 = c(1,
1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), MP470 = c(1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1), MPS.1.IN.1 = c(1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), NG.25 = c(1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), Nilotinib = c(1,
1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1), NPK76.II.72.1 = c(1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), NSC.207895 = c(1,
1, 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 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 0), PF.4708671 = c(1,
1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), PF.562271 = c(1,
1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1), PFI.1 = c(1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), PHA.793887 = c(1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), Phenformin = c(1,
1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1), PI.103 = c(1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), PIK.93 = c(1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), piperlongumine = c(1,
1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1), PLX4720 = c(1,
1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), PLX4720.rescreen. = c(1,
1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), PXD101.Belinostat = c(1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), QL.X.138 = c(1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), QL.XI.92 = c(1,
1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1), QL.XII.47 = c(1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), QS11 = c(1,
1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), RDEA119 = c(1,
1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), RDEA119.rescreen. = c(1,
1, 1, 1, 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, 1, 1, 1, 1, 1), Temozolomide = c(1,
1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1), Temsirolimus = c(1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), TG101348 = c(1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), Thapsigargin = c(1,
1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1), THZ.2.102.1 = c(1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), THZ.2.49 = c(1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), Tipifarnib = c(1,
1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1), TL.1.85 = c(1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), TL.2.105 = c(1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), TPCA.1 = c(1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), Trametinib = c(1,
1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1), TubastatinA = c(1,
1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1), UNC0638 = c(1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), UNC0638.1 = c(1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), UNC1215 = c(1,
1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), Vinblastine = c(1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), Vinorelbine = c(1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 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4.279 = c(1,
1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), YM155 = c(1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), YM201636 = c(1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), Zibotentan.ZD4054 = c(1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), ZM.447439 = c(1,
1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), ZSTK474 = c(1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)), row.names = c("BT-20",
"CAL-120", "CAL-51", "CAL-85-1", "DU-4475", "HCC1143", "HCC1187",
"HCC1395", "HCC1937", "HCC2157", "HCC38", "HDQ-P1", "Hs-578-T",
"MDA-MB-231", "MDA-MB-436", "MDA-MB-468", "MFM-223"), class = "data.frame")
Not really sure, because I did not fully understand your desired output...
But here is a go using the data.table-package
#load library
library(data.table)
#first, create the sample data
#note, rownames are stored in their own column, 'row_name'
dt <- fread("row_name ABT.263 ABT.869 ABT.888 AC220 Afatinib Afatinib.rescreen.
BT-20 1 1 1 1 0 1
CAL-120 0 1 1 1 1 1
CAL-51 1 1 1 1 1 1
CAL-85-1 1 1 1 1 1 0
DU-4475 0 1 1 1 1 1
HCC1143 1 1 1 1 0 1")
#melt the data to long format, using the 'row_name'-column as identifier
dt.melt <- melt( dt, id.vars = "row_name" )
#now summarise, pasting together row_names by variable-value combination
dt.melt[, .( row = paste0( row_name, collapse = ";" ) ),
by = .( value = paste0( variable, "_", value ) ) ]
# value row
# 1: ABT.263_1 BT-20;CAL-51;CAL-85-1;HCC1143
# 2: ABT.263_0 CAL-120;DU-4475
# 3: ABT.869_1 BT-20;CAL-120;CAL-51;CAL-85-1;DU-4475;HCC1143
# 4: ABT.888_1 BT-20;CAL-120;CAL-51;CAL-85-1;DU-4475;HCC1143
# 5: AC220_1 BT-20;CAL-120;CAL-51;CAL-85-1;DU-4475;HCC1143
# 6: Afatinib_0 BT-20;HCC1143
# 7: Afatinib_1 CAL-120;CAL-51;CAL-85-1;DU-4475
# 8: Afatinib.rescreen._1 BT-20;CAL-120;CAL-51;DU-4475;HCC1143
# 9: Afatinib.rescreen._0 CAL-85-1
A base R solution with a list of vectors is also possible:
Code:
names <- names(df)
#
l1 <- list()
for(i in 1:length(df)){
l1[[i]] <- rownames(df)[which(df[,names[i]] == 1)]
}
Result
l1
[[1]]
[1] "BT-20" "CAL-51" "CAL-85-1" "HCC1143"
[[2]]
[1] "BT-20" "CAL-120" "CAL-51" "CAL-85-1" "DU-4475" "HCC1143"
[[3]]
[1] "BT-20" "CAL-120" "CAL-51" "CAL-85-1" "DU-4475" "HCC1143"
[[4]]
[1] "BT-20" "CAL-120" "CAL-51" "CAL-85-1" "DU-4475" "HCC1143"
[[5]]
[1] "CAL-120" "CAL-51" "CAL-85-1" "DU-4475"
[[6]]
[1] "BT-20" "CAL-120" "CAL-51" "DU-4475" "HCC1143"
This is the list of names with 1 in the data.frame, the list of 0's could be obtained identically.