Regex parameter in extract function in R - r

According to solution in Gather multiple sets of columns I am trying to gather multiple sets of columns. All columns which I would like to gather have year in the end of the sentece. column.name.2010, column.name.2011, column.name.2012, column.name2.2010, column.name2.2011, column.name2.2012 etc.
I tried
db %>%
gather(key, value, -c(1:6)) %>%
extract(key, c("question", "Year"), "([^\d{4}]+)") %>%
spread(question, value)
I would like to accomplish this:
Id Index.number Country.Name World.Rank.by.Assets Year column.name. column.name2.
1 a 3 6 2010 xxxx xxy
2 b 4 6 2011 xxyx xyy
3 c 5 8 2012 xxxs sada
Head of db
Id Index.number Country.Name World.Rank.by.Assets Total.Assets.th.EUR.2010 Total.Assets.th.EUR.2011
1 10931 10931 9 6 n.a. 1,965,283,000
2 10696 10696 9 9 n.a. 1,879,536,000
3 11948 11948 9 13 n.a. 1,723,608,000
4 11150 11150 9 19 n.a. 1,181,372,000
5 10708 10708 9 NA n.a. 1,138,395,000
Total.Assets.th.EUR.2012 Total.Assets.th.EUR.2013 Total.Assets.th.EUR.2014 Total.Assets.th.EUR.2015
1 1,907,200,000 1,810,522,000 2,077,759,000 1,994,193,000
2 1,783,220,000 1,688,264,000 1,762,714,000 1,698,859,000
3 1,617,429,000 1,518,811,000 1,589,044,000 1,529,294,000
4 1,250,889,000 1,214,193,000 1,308,138,000 1,334,391,000
5 1,147,521,000 1,123,520,000 1,223,298,000 1,166,535,000
Total.Assets.th.EUR.2016 Deposits...Short.term.funding.th.EUR.2010
1 2,076,959,000 n.a.
2 1,722,849,000 n.a.
3 1,524,232,000 n.a.
4 1,382,241,000 n.a.
5 1,235,240,000 n.a.
Deposits...Short.term.funding.th.EUR.2011 Deposits...Short.term.funding.th.EUR.2012
1 947,516,000 942,644,000
2 917,933,000 928,045,000
3 808,028,000 805,504,000
4 452,417,000 540,628,000
5 604,912,000 661,297,000
Deposits...Short.term.funding.th.EUR.2013 Deposits...Short.term.funding.th.EUR.2015
1 942,387,000 1,032,100,000
2 911,884,000 891,035,000
3 789,353,000 796,777,000
4 523,624,000 673,520,000
5 692,997,000 707,185,000
Deposits...Short.term.funding.th.EUR.2014 Deposits...Short.term.funding.th.EUR.2016
1 1,031,624,000 1,111,246,000
2 890,072,000 903,317,000
3 773,094,000 759,184,000
4 587,177,000 687,328,000
5 740,298,000 761,140,000
Loan.loss.res...Gross.loans...2010 Loan.loss.res...Gross.loans...2011 Loan.loss.res...Gross.loans...2012
1 n.a. 4,04 4,05
2 n.a. 3,55 3,16
3 n.a. 4,53 3,77
4 n.a. 4,21 4,26
5 n.a. 1,96 2,03

Related

Filter NA containing rows into a new data frame in R

My dummy data frame for gene expression is this
**FRESHUPDATE**
My data frame small one from my bigger gene expression which i want to filter
My small subset is this mat2
Symbol TCGA.AB.2856 TCGA.AB.2849 TCGA.AB.2971 TCGA.AB.2930 TCGA.AB.2891 TCGA.AB.2872 TCGA.AB.2851 TCGA.AB.3011 TCGA.AB.2949
1 A2ML1 4.627857365 5.369632 6.700112904 5.6232636 4.75680637 5.8050996 6.2077827 5.2683007 5.232384
2 A4GALT 5.550918500 5.572321 4.569849528 6.2627817 5.25197103 6.4728585 3.8088796 5.5766959 6.458113
3 AACSP1 -0.004394347 1.195122 -0.004562859 0.1343311 -0.01469569 0.2808245 0.2881929 0.3270398 0.708931
4 ABCA9 5.652068819 5.579944 7.787378888 4.9460252 4.77917651 5.5384349 5.6242293 5.8726373 8.846332
5 ABCA9-AS1 0.557163318 1.701202 3.343076301 0.4203761 1.04232725 0.5324808 1.3794852 1.9304208 3.594210
6 ABCC13 4.077316070 8.840604 2.340835263 3.0782108 2.32162741 4.0645558 3.3683787 4.0456838 3.129047
7 ABLIM1 9.696391499 11.988791 9.873324476 10.5111442 10.81262360 9.0651002 10.6804131 9.4307673 11.879929
8 ABLIM3 5.292492658 5.979259 3.623770183 3.5016803 6.74841153 4.9092703 3.7786797 3.9352033 4.406261
9 ABO 10.631505004 6.859666 5.505456740 10.1379316 6.39110235 10.2743712 9.9307084 6.3601978 11.161422
10 ACOT12 1.648498344 3.762861 2.098422076 1.1439361 2.39612635 2.0490598 0.8765957 2.6902788 2.896370
>
To get std dev i did this
mat1 <- mat2
mat1[,-1] <- lapply(mat1[,-1],
function(x) replace(x,abs(scale(x))>2,NA))
The to find rows with any NA I did this
mat_rown <- mat1 %>% remove_rownames %>% column_to_rownames(var="Symbol")
which(is.na(mat_rown),arr.ind = TRUE)
Which gives me this data frame
Symbol TCGA.AB.2856 TCGA.AB.2849 TCGA.AB.2971 TCGA.AB.2930 TCGA.AB.2891 TCGA.AB.2872 TCGA.AB.2851 TCGA.AB.3011 TCGA.AB.2949
1 A2ML1 4.627857 5.369632 6.700113 5.623264 4.756806 5.805100 6.207783 5.268301 5.232384
2 A4GALT 5.550918 5.572321 4.569850 6.262782 5.251971 6.472859 3.808880 5.576696 6.458113
3 AACSP1 NA NA NA NA NA NA NA NA NA
4 ABCA9 5.652069 5.579944 NA 4.946025 4.779177 5.538435 5.624229 5.872637 8.846332
5 ABCA9-AS1 NA NA 3.343076 NA NA NA 1.379485 NA 3.594210
6 ABCC13 4.077316 8.840604 2.340835 3.078211 2.321627 4.064556 3.368379 4.045684 3.129047
7 ABLIM1 NA NA NA NA NA NA NA NA NA
8 ABLIM3 5.292493 5.979259 3.623770 3.501680 NA 4.909270 3.778680 3.935203 4.406261
9 ABO NA 6.859666 5.505457 NA NA NA NA 6.360198 NA
10 ACOT12 1.648498 3.762861 2.098422 1.143936 2.396126 2.049060 NA 2.690279 2.896370
here we can see these genes they have some or the other NA in different columns so my objective is to take out these rows .
So when Im trying to index those rows with NA
AACSP1,ABCA9-AS1,ABLIM1,ABO,ACOT12
I get these
row col
AACSP1 3 1
ABCA9-AS1 5 1
ABLIM1 7 1
ABO 9 1
AACSP1 3 2
ABCA9-AS1 5 2
ABLIM1 7 2
AACSP1 3 3
ABCA9 4 3
ABLIM1 7 3
AACSP1 3 4
ABCA9-AS1 5 4
ABLIM1 7 4
ABO 9 4
AACSP1 3 5
ABCA9-AS1 5 5
ABLIM1 7 5
ABLIM3 8 5
ABO 9 5
AACSP1 3 6
ABCA9-AS1 5 6
ABLIM1 7 6
ABO 9 6
AACSP1 3 7
ABLIM1 7 7
ABO 9 7
ACOT12 10 7
AACSP1 3 8
ABCA9-AS1 5 8
ABLIM1 7 8
AACSP1 3 9
ABLIM1 7 9
ABO 9 9
So my simple idea is to preserve these NA containing rows or genes in another data frame or object which i can further use downstream for my different analysis to check
If you simply are trying to split your original frame into those that have a low vs high row standard deviation, you can do this:
rld2 <- as.data.frame((mat)) %>% rownames_to_column('gene')
# set your threshold that defines "high" deviation (i've picked a relatively low one here; you might choose something like 3)
sd_threshold = .6
# get the row-specific standard deviation, using `apply()`
row_sds = apply(rld2[,-1],1, \(r) sd(r))
# split into a list of two frames,
low_high_split <- split(rld2, f = row_sds>sd_threshold)

how to extract observation values for each cluster of Kmeans

I have data that come from two distribution functions (mixture data). I fit the k-means to the data with $2$ centers. I then get the clusters. My point here is, instead of the number of each cluster, I would like to divide my data into two groups. That is, the first group contains the data that comes from the first cluster and the same for the second group (my data is two dimensions and a matrix).
Here is my try:
kme <- kmeans(Sim, 2)
kme$cluster
which gives this:
kme$cluster
[1] 1 2 2 1 1 1 2 2 2 1 2 2 1 2 1 2 1 2 2 1 2 1 2 2 2 1 2 1 2 1 1 2 1 1 1 2 2 1 1 1 1 1 2 2 1 1 1 2 2 1 2 1 2 2 2
[56] 1 2 1 2 2 1 2 1 1 2 2 1 2 2 1 1 2 2 1 2 2 1 1 1 2 2 2 2 2 2 1 2 2 2 1 2 2 2 2 1 1 2 2 1 2
I know that means the first row (observations in the first row) of my matrix comes from the first cluster and the second and third rows are from the second cluster. Instead of this, I want two groups, one with the observations (the values not the number of the cluster) of the first cluster, and the other come from the second cluster.
For example,
[,1] [,2] [,3]
[1,] 0.8026952 0.8049413 1
[2,] 0.4333745 0.5063472 2
[3,] 0.3587946 0.4091627 2
[4,] 0.9067146 0.9211618 1
[5,] 0.6663730 0.6644439 1
[6,] 0.9752217 0.8299001 1
Hence, I want it like this:
Group_1
[,1] [,2]
[1,] 0.8026952 0.8049413
[2,] 0.9067146 0.9211618
[3,] 0.6663730 0.6644439
[4,] 0.9752217 0.8299001
Group_2
[2,] 0.4333745 0.5063472
[3,] 0.3587946 0.4091627
## my data
structure(c(0.8026952064848, 0.433374540465373, 0.35879457564118,
0.906714606331661, 0.666372966486961, 0.975221659988165, 0.146514602801487,
0.185211665343342, 0.266845172200967, 0.9316249943804, 0.458760005421937,
0.260092565789819, 0.546946153900359, 0.320214906940237, 0.998543527442962,
0.264783770404576, 0.940526409307495, 0.218771387590095, 0.00109510733232848,
0.909367726704406, 0.195467973826453, 0.853418850837688, 0.257240866776556,
0.18492349224921, 0.0350681275368262, 0.743108308431699, 0.120800079312176,
0.536067422405767, 0.387076289858669, 0.859893148997799, 0.962759922724217,
0.0288314732712864, 0.878663770621642, 0.98208610656754, 0.98423704248853,
0.0850008164197942, 0.415692074922845, 0.725441533140838, 0.514739896170795,
0.564903213409707, 0.65493689605431, 0.551635805051774, 0.20452569425106,
0.0509099354967475, 0.646801606381046, 0.656341063790023, 0.706781879998744,
0.244539211907925, 0.43318469475677, 0.848426640266553, 0.26359805940462,
0.730860544172275, 0.405211122473702, 0.401496034115553, 0.432796132021846,
0.654138915939257, 0.00803712895140052, 0.991968845921972, 0.0311756118742527,
0.0648601313587278, 0.733741108178729, 0.0431173096876591, 0.619796682847664,
0.804308546474203, 0.0934691624715924, 0.520366458455101, 0.833598382357762,
0.373484763782471, 0.261487311183624, 0.822368689114228, 0.88254910800606,
0.261728620579622, 0.109025254459585, 0.661885950024542, 0.231851563323289,
0.46855820226483, 0.909970719134435, 0.799321972066537, 0.646252158097923,
0.233985049184412, 0.309839888018159, 0.129971102112904, 0.0901338488329202,
0.460395671925082, 0.274646409088746, 0.675003502921675, 0.00289221783168614,
0.336108531044562, 0.371105678845197, 0.607435576152056, 0.156731446506456,
0.246894558891654, 0.418194083335386, 0.000669385509081014, 0.929943428778418,
0.972200238145888, 0.503282874496368, 0.126382717164233, 0.683936105109751,
0.21720214970307, 0.804941252722838, 0.506347232734472, 0.409162739287115,
0.921161751145135, 0.664443932378791, 0.829900114789874, 0.0660539097664178,
0.296326436845226, 0.120007439729838, 0.768823563807157, 0.449026418114183,
0.268668511775742, 0.733763495587273, 0.365402223476625, 0.97980160509396,
0.335119241818387, 0.929315469866307, 0.253016166717649, 0.00521095494948787,
0.870041067705, 0.215020805969677, 0.858896143709886, 0.167998804405928,
0.204213777320881, 0.050652931423494, 0.731499125526297, 0.166061290725948,
0.520575411719918, 0.370579454420263, 0.655607928337889, 0.978414469097905,
0.00268175014874324, 0.937587480238656, 0.992468047261219, 0.856301580636229,
0.106064732119751, 0.530228247677302, 0.502227925225818, 0.66462369930413,
0.526988978414104, 0.394591213637187, 0.623968017885322, 0.222666427921132,
0.0707407196787662, 0.715361864683925, 0.561951996212598, 0.874765155771585,
0.217631973951671, 0.576708062239157, 0.910641489550344, 0.215463715360162,
0.761807500922947, 0.417110771840405, 0.497162608159201, 0.530665309105489,
0.689703677933362, 0.00811876221245061, 0.991245541114815, 0.0518070069187705,
0.0733367055960226, 0.803126294581356, 0.0291602667026993, 0.724848517465592,
0.682316094846719, 0.0914714514707226, 0.426956537783392, 0.826985575416605,
0.3128962286514, 0.295208624024388, 0.58934716401092, 0.856718183582533,
0.183019143019377, 0.302561606994597, 0.666755501118539, 0.176298329811281,
0.389183841328174, 0.86253900906311, 0.753736534075238, 0.627220192419063,
0.319958512526359, 0.321602248149364, 0.161772830672492, 0.103166641060684,
0.339980194505715, 0.218533019046996, 0.689884789678819, 0.00251942038852481,
0.174792447835404, 0.509071373135409, 0.647835095901117, 0.22572898134156,
0.287369659385574, 0.538675651472693, 0.000995476493411555, 0.939528694637273,
0.961510166904661, 0.452822116916426, 0.2061782381611, 0.722694525115558,
0.328404467661884), .Dim = c(100L, 2L))
I hope this is what you are looking for.
I had to transform the matrix to a data frame so that when we use split function the structure will be preserved, otherwise it would split the whole matrix element by element as matrix is actually a vector that has dim attribute. So it behaves like a vector
split function divides a data frame or a vector into groups defined by f. which in your case are unique cluster values
kme <- kmeans(Sim, 2)
kme$cluster
Sim2 <- as.data.frame(cbind(Sim, kme$cluster))
split(Sim2, Sim2$V3) |>
setNames(paste("Group", sort(unique(kme$cluster))))
$`Group 1`
V1 V2 V3
2 0.4333745405 0.5063472327 1
3 0.3587945756 0.4091627393 1
7 0.1465146028 0.0660539098 1
8 0.1852116653 0.2963264368 1
9 0.2668451722 0.1200074397 1
11 0.4587600054 0.4490264181 1
12 0.2600925658 0.2686685118 1
14 0.3202149069 0.3654022235 1
16 0.2647837704 0.3351192418 1
18 0.2187713876 0.2530161667 1
19 0.0010951073 0.0052109549 1
21 0.1954679738 0.2150208060 1
23 0.2572408668 0.1679988044 1
24 0.1849234922 0.2042137773 1
25 0.0350681275 0.0506529314 1
27 0.1208000793 0.1660612907 1
29 0.3870762899 0.3705794544 1
32 0.0288314733 0.0026817501 1
36 0.0850008164 0.1060647321 1
37 0.4156920749 0.5302282477 1
43 0.2045256943 0.2226664279 1
44 0.0509099355 0.0707407197 1
48 0.2445392119 0.2176319740 1
49 0.4331846948 0.5767080622 1
51 0.2635980594 0.2154637154 1
53 0.4052111225 0.4171107718 1
54 0.4014960341 0.4971626082 1
55 0.4327961320 0.5306653091 1
57 0.0080371290 0.0081187622 1
59 0.0311756119 0.0518070069 1
60 0.0648601314 0.0733367056 1
62 0.0431173097 0.0291602667 1
65 0.0934691625 0.0914714515 1
66 0.5203664585 0.4269565378 1
68 0.3734847638 0.3128962287 1
69 0.2614873112 0.2952086240 1
72 0.2617286206 0.1830191430 1
73 0.1090252545 0.3025616070 1
75 0.2318515633 0.1762983298 1
76 0.4685582023 0.3891838413 1
80 0.2339850492 0.3199585125 1
81 0.3098398880 0.3216022481 1
82 0.1299711021 0.1617728307 1
83 0.0901338488 0.1031666411 1
84 0.4603956719 0.3399801945 1
85 0.2746464091 0.2185330190 1
87 0.0028922178 0.0025194204 1
88 0.3361085310 0.1747924478 1
89 0.3711056788 0.5090713731 1
91 0.1567314465 0.2257289813 1
92 0.2468945589 0.2873696594 1
93 0.4181940833 0.5386756515 1
94 0.0006693855 0.0009954765 1
97 0.5032828745 0.4528221169 1
98 0.1263827172 0.2061782382 1
100 0.2172021497 0.3284044677 1
$`Group 2`
V1 V2 V3
1 0.8026952 0.8049413 2
4 0.9067146 0.9211618 2
5 0.6663730 0.6644439 2
6 0.9752217 0.8299001 2
10 0.9316250 0.7688236 2
13 0.5469462 0.7337635 2
15 0.9985435 0.9798016 2
17 0.9405264 0.9293155 2
20 0.9093677 0.8700411 2
22 0.8534189 0.8588961 2
26 0.7431083 0.7314991 2
28 0.5360674 0.5205754 2
30 0.8598931 0.6556079 2
31 0.9627599 0.9784145 2
33 0.8786638 0.9375875 2
34 0.9820861 0.9924680 2
35 0.9842370 0.8563016 2
38 0.7254415 0.5022279 2
39 0.5147399 0.6646237 2
40 0.5649032 0.5269890 2
41 0.6549369 0.3945912 2
42 0.5516358 0.6239680 2
45 0.6468016 0.7153619 2
46 0.6563411 0.5619520 2
47 0.7067819 0.8747652 2
50 0.8484266 0.9106415 2
52 0.7308605 0.7618075 2
56 0.6541389 0.6897037 2
58 0.9919688 0.9912455 2
61 0.7337411 0.8031263 2
63 0.6197967 0.7248485 2
64 0.8043085 0.6823161 2
67 0.8335984 0.8269856 2
70 0.8223687 0.5893472 2
71 0.8825491 0.8567182 2
74 0.6618860 0.6667555 2
77 0.9099707 0.8625390 2
78 0.7993220 0.7537365 2
79 0.6462522 0.6272202 2
86 0.6750035 0.6898848 2
90 0.6074356 0.6478351 2
95 0.9299434 0.9395287 2
96 0.9722002 0.9615102 2
99 0.6839361 0.7226945 2
Add the kme$cluster values to the original dataframe and then create a new dataframe with each column based on the value in kme$cluster
From what I understand without a data sample:
library(tidyverse)
Sim <- Sim %>%
mutate(cluster_group = kme$cluster)
df_final <- data.frame(Group1 = Sim %>%
filter(cluster_group == 1) %>%
select(value) %>%
pull(),
Group2 = Sim %>%
filter(cluster_group== 2) %>%
select(value) %>%
pull())
With value the values used for the kmeans in Sim

R - Skip columns in pmax command if they do not exist

I'd like to use the pmax command to create a new column. My code Looks like this:
Master <- Master %>%
mutate(RAM = pmax(RAM1, RAM2, RAM3, RAM4, RAM5, RAM6, RAM7, RAM8, RAM9, RAM10,
RAM11, RAM12, RAM13, RAM14, RAM15, RAM16, RAM17, RAM18,
RAM19, RAM20, RAM21, RAM22, RAM23, RAM24, RAM25, RAM26,
RAM27, RAM28, RAM29, RAM30, RAM31, RAM32, RAM33, RAM34,
RAM35, RAM36, RAM37, RAM38, RAM39, RAM40, RAM41, RAM42,
RAM43, RAM44, RAM45, RAM46, RAM47, RAM48, RAM49, RAM50,
RAM51, RAM52, RAM53, RAM54, RAM55, RAM56, RAM57, RAM58,
RAM59, RAM60, RAM61, RAM62, RAM63, RAM64, RAM65, RAM66,
RAM67, RAM68, RAM69, RAM70, RAM71, RAM72, RAM73, RAM74,
RAM75, RAM76, RAM77, RAM78, RAM79, RAM80, RAM81, RAM82,
RAM83, RAM84, RAM85, RAM86, RAM87, RAM88, RAM89, RAM90,
RAM91, RAM92, na.rm =T))
In my current data base, however, only the columns RAM1 to RAM8 exist. In this case, I want R to skip all the other columns mentioned in the Statement and to only use column RAM1 to RAM8 (it is okay if R displays an error message, but I don't want the program to interrupt running the code).
Any ideas how to do so?
Thanks!
One way to do this would be as follows:
Set up some data to make a reproducible example
set.seed(0)
Master <- data.frame(Other=100,RAM1=1:10, RAM2=1:10, RAM3=1:10, RAM4=1:10,
RAM5=1:10, RAM6=1:10, RAM7=1:10, RAM8=rnorm(10)+5)
Master[5,5] <- NA
Select required columns of the dataframe:
Master[colnames(Master) %in% paste0("RAM",1:92)]
Use do.call to run pmax using the selected columns as arguments, and adding the argument na.rm=TRUE
Master$RAM <- do.call(pmax, c(Master[colnames(Master) %in% paste0("RAM",1:92)], na.rm=TRUE))
Sample output:
Master
# Other RAM1 RAM2 RAM3 RAM4 RAM5 RAM6 RAM7 RAM8 RAM
#1 100 1 1 1 1 1 1 1 6.262954 6.262954
#2 100 2 2 2 2 2 2 2 4.673767 4.673767
#3 100 3 3 3 3 3 3 3 6.329799 6.329799
#4 100 4 4 4 4 4 4 4 6.272429 6.272429
#5 100 5 5 5 NA 5 5 5 5.414641 5.414641
#6 100 6 6 6 6 6 6 6 3.460050 6.000000
#7 100 7 7 7 7 7 7 7 4.071433 7.000000
#8 100 8 8 8 8 8 8 8 4.705280 8.000000
#9 100 9 9 9 9 9 9 9 4.994233 9.000000
#10 100 10 10 10 10 10 10 10 7.404653 10.000000

Extracting data from dataframe using different dataframe without headers (R)

I have a gridded data as a data-frame that has daily temperatures (in K) for 30 years. I need to extract data for days that matches another data-frame and keep the first and second columns (lon and lat).
Data example:
gridded data from which I need to remove days that do not match days in the second data (df2$Dates)
>head(Daily.df)
lon lat 1991-05-01 1991-05-02 1991-05-03 1991-05-04 1991-05-05 1991-05-06 1991-05-07 1991-05-08 1991-05-09
1 5.000 60 278.2488 280.1225 280.3909 279.4138 276.6809 276.2085 276.6250 277.7930 276.9693
2 5.125 60 278.2514 280.1049 280.3789 279.4395 276.7141 276.2467 276.6571 277.8264 277.0225
3 5.250 60 278.2529 280.0871 280.3648 279.4634 276.7437 276.2849 276.6918 277.8608 277.0740
4 5.375 60 278.2537 280.0687 280.3488 279.4858 276.7691 276.3238 276.7289 277.8960 277.1232
5 5.500 60 278.2537 280.0493 280.3319 279.5066 276.7909 276.3633 276.7688 277.9313 277.1701
6 5.625 60 278.2539 280.0294 280.3143 279.5264 276.8090 276.4042 276.8111 277.9666 277.2147
1991-05-10 1991-05-11 1991-05-12 1991-05-13 1991-05-14 1991-05-15 1991-05-16 1991-05-17 1991-05-18 1991-05-19
1 276.9616 277.3436 273.3149 274.4931 274.6967 275.6298 272.2511 271.5413 271.7289 271.7964
2 276.9689 277.2988 273.3689 274.5399 274.6801 275.6307 272.2214 271.4445 271.6410 271.7023
3 276.9720 277.2533 273.4225 274.5811 274.6646 275.6241 272.1858 271.3391 271.5424 271.5989
4 276.9716 277.2080 273.4726 274.6146 274.6507 275.6109 272.1456 271.2274 271.4340 271.4872
5 276.9689 277.1632 273.5163 274.6382 274.6380 275.5917 272.1022 271.1121 271.3168 271.3693
6 276.9645 277.1190 273.5507 274.6501 274.6263 275.5672 272.0571 270.9955 271.1919 271.2469
1991-05-20 1991-05-21 1991-05-22 1991-05-23 1991-05-24 1991-05-25 1991-05-26 1991-05-27 1991-05-28 1991-05-29
1 272.2633 268.0039 268.5981 269.4139 267.7836 265.8771 263.5669 266.1666 269.7285 272.5083
2 272.2543 268.0218 268.5847 269.4107 267.7886 265.8743 263.5125 266.1031 269.6471 272.4676
3 272.2434 268.0369 268.5716 269.4089 267.7910 265.8669 263.4592 266.0332 269.5697 272.4217
4 272.2308 268.0507 268.5597 269.4090 267.7925 265.8559 263.4066 265.9581 269.4987 272.3714
5 272.2164 268.0642 268.5505 269.4112 267.7936 265.8425 263.3546 265.8797 269.4355 272.3175
6 272.2005 268.0793 268.5451 269.4154 267.7962 265.8276 263.3039 265.7997 269.3818 272.2614
1991-05-30 1991-05-31 1991-06-01 1991-06-02 1991-06-03 1991-06-04 1991-06-05 1991-06-06 1991-06-07 1991-06-08
1 274.2950 273.4715 274.5197 274.7548 273.8259 272.4433 274.1811 274.4135 274.3999 276.0327
2 274.2205 273.4638 274.5292 274.8316 273.8658 272.4700 274.1992 274.4426 274.4650 276.0698
3 274.1421 273.4549 274.5373 274.9027 273.9028 272.4980 274.2160 274.4781 274.5309 276.1012
4 274.0609 273.4452 274.5438 274.9665 273.9365 272.5273 274.2322 274.5211 274.5969 276.1255
5 273.9784 273.4353 274.5482 275.0216 273.9660 272.5576 274.2481 274.5725 274.6617 276.1417
6 273.8960 273.4253 274.5508 275.0668 273.9912 272.5887 274.2649 274.6334 274.7239 276.1487
1991-06-09 1991-06-10 1991-06-11 1991-06-12 1991-06-13 1991-06-14 1991-06-15 1991-06-16 1991-06-17 1991-06-18
1 276.5216 277.1812 277.8093 278.3013 278.5323 278.5403 277.9563 278.3461 275.8296 273.8277
2 276.5531 277.1925 277.8261 278.3409 278.4956 278.5317 277.9148 278.3234 275.8167 273.8302
3 276.5861 277.2065 277.8457 278.3748 278.4503 278.5181 277.8654 278.2939 275.8057 273.8358
4 276.6204 277.2239 277.8684 278.4029 278.3988 278.4996 277.8080 278.2583 275.7966 273.8427
5 276.6564 277.2466 277.8945 278.4253 278.3423 278.4759 277.7429 278.2171 275.7888 273.8504
6 276.6938 277.2753 277.9242 278.4414 278.2834 278.4472 277.6715 278.1714 275.7819 273.8570
1991-06-19 1991-06-20 1991-06-21 1991-06-22 1991-06-23 1991-06-24 1991-06-25 1991-06-26 1991-06-27 1991-06-28
1 275.1738 274.6805 275.6100 274.8936 273.5818 273.2099 273.1788 271.2747 273.2458 276.9931
2 275.1808 274.7123 275.7043 274.9494 273.5861 273.1770 273.2280 271.2435 273.2662 276.9822
3 275.1859 274.7478 275.7993 275.0009 273.5956 273.1439 273.2730 271.2133 273.2803 276.9678
4 275.1891 274.7879 275.8941 275.0467 273.6107 273.1106 273.3130 271.1840 273.2886 276.9502
5 275.1902 274.8337 275.9870 275.0857 273.6318 273.0777 273.3472 271.1556 273.2918 276.9307
6 275.1891 274.8864 276.0776 275.1168 273.6589 273.0454 273.3752 271.1285 273.2905 276.9101
1991-06-29 1991-06-30
1 272.0784 273.5677
2 272.0577 273.5973
3 272.0339 273.6237
4 272.0075 273.6476
5 271.9794 273.6701
6 271.9500 273.6925
Second data I'm using for extracting (using Dates variable)
>head(df2)
Dates Temp Wind.S Wind.D
1 5/1/1991 18 4 238
2 5/2/1991 18 8 93
3 5/4/1991 22 8 229
4 5/6/1991 21 4 81
5 5/7/1991 21 8 192
6 5/9/1991 17 8 32
7 5/13/1991 22 8 229
8 5/18/1991 21 4 81
9 6/2/1991 21 8 192
10 6/7/1991 17 8 32
The header of the final data I'm looking for is something like this:
>head(df3)
lon lat 1991-05-01 1991-05-02 1991-05-04 1991-05-06 1991-05-09 1991-05-13
Example data following the format of yours
Daily.df <- data.frame(lon=1:5,lat=1:5,A=1:5,B=1:5,C=1:5,D=1:5)
colnames(Daily.df) <- c("lon","lat","1991-05-01","1991-05-02","1991-05-03","1991-05-04")
lon lat 1991-05-01 1991-05-02 1991-05-03 1991-05-04
1 1 1 1 1 1 1
2 2 2 2 2 2 2
3 3 3 3 3 3 3
4 4 4 4 4 4 4
5 5 5 5 5 5 5
df2 <- data.frame(Dates = c("5/1/1991","5/2/1991","5/4/1991"))
Dates
1 5/1/1991
2 5/2/1991
3 5/4/1991
Using lubridate to convert df2$Dates into the right format, make a vector of the column names you want to keep (thesedates) including lon and lat. Then use select_at to keep those columns.
library(lubridate)
library(dplyr)
thesedates <- c("lon","lat",as.character(mdy(df2$Dates)))
new.df <- Daily.df %>%
select_at(vars(thesedates))
Output
lon lat 1991-05-01 1991-05-02 1991-05-04
1 1 1 1 1 1
2 2 2 2 2 2
3 3 3 3 3 3
4 4 4 4 4 4
5 5 5 5 5 5
If you want to have a long data set to match, I would think you need to first convert the dates in df2 into the proper format and then wrangle the data into wide format.
Step 1 - Convert dates into correct format
df2$Dates <- as.Date(df2$Dates, format = "%m/%d/%Y")
Step 2 - convert to wide format
library(tidyr)
spread(df2, Dates, data)

extract rows with common characters in a column by comparing two data.frame

How can compare two data.frame (df1 and df2) and extract the rows with common gene names
df1 =
logp chr start end CNA Genes No.of.genes
25.714.697 1 90100868 90212160 gain Iqca,Ackr3 2
2.213.423 1 175422136 176019087 loss Rgs7,Fh1,Kmo,Opn3,Chml,Wdr64,Gm25560,Exo1,Gm23805,Pld5,B020018G12Rik 11
5.607.005 2 145619035 147312698 gain Slc24a3,Rin2,Naa20,Crnkl1,4930529M08Rik,Insm1,Ralgapa2,Xrn2,Nkx2-4,Nkx2-2,Gm22261 11
3.756.075 2 141246149 141653989 loss Macrod2 1
4.852.608 2 41586450 41739605 loss Lrp1b 1
590.684 2 86729423 86860061 loss Olfr1089,Olfr1090,Olfr1093,Olfr1093,Olfr141,Olfr1094,Olfr1094,Olfr1095 8
5.721.239 3 25408115 25519319 gain Nlgn1 1
4.295.527 3 92005564 92134972 gain Pglyrp3,Prr9 2
4.257.749 3 15244004 15897870 gain Gm9733,Gm9733,Gm9733,Gm9733,Sirpb1a,Sirpb1a,Sirpb1a,Sirpb1a,Sirpb1b,Sirpb1b,Sirpb1b,Sirpb1b,Sirpb1c,Sirpb1c,Sirpb1c,Sirpb1c 16
418.259 3 154861710 155490219 loss Tnni3k,Tnni3k,Fpgt,Gm26456,Lrriq3 5
2.284.327 4 134885344 137474898 gain Rhd,Rhd,Tmem50a,D4Wsu53e,Syf2,Runx3,Clic4,Srrm1,Ncmap,Rcan3,Nipal3,Stpg1,Gm25317,Grhl3,Gm23106,Ifnlr1,Il22ra1,Myom3,Srsf10,Pnrc2,Pnrc2,Cnr2,Fuca1,Hmgcl,Gale,Lypla2,Pithd1,Tceb3,Rpl11,Gm26001,Id3,E2f2,Asap3,Tcea3,Zfp46,Hnrnpr,Htr1d,Luzp1,Kdm1a,4930549C01Rik,Lactbl1,Ephb2,C1qb,C1qc,C1qa,Epha8,Zbtb40,Gm23834,Gm23834,Wnt4,Cdc42,Gm13011,Gm13011,Cela3b,Cela3b,Hspg2 56
1.017.899 4 108176679 108417038 gain Echdc2,Zyg11a,Zyg11b,Selrc1,Fam159a,Gpx7 6
2.229.929 4 80406963 83998058 gain Tyrp1,Lurap1l,Mpdz,n-R5s187,Nfib,Zdhhc21,Cer1,Frem1,Ttc39b,Gm23412,Snapc3,Psip1,Ccdc171,Gm25899,Gm25899 15
279.458 4 110534756 110628705 gain Agbl4 1
1.103.167 4 121565222 124833802 gain Ppt1,Cap1,Mfsd2a,Mycl,Trit1,Bmp8b,Bmp8b,Oxct2b,Ppie,Hpcal4,Nt5c1a,Heyl,Pabpc4,Gm25788,Gm22154,Bmp8a,Bmp8a,Oxct2a,Macf1,Ndufs5,Akirin1,Rhbdl2,Mycbp,Rragc,Gm22983,Pou3f1,Utp11l,Gm24480,Fhl3,Sf3a3,Inpp5b,Mtf1,n-R5s192 33
1.781.441 4 139917291 140083763 loss Klhdc7a,Igsf21 2
6.829.744 6 147086557 147179673 gain Mansc4,Klhl42 2
1.070.905 6 63350920 64077379 loss Grid2 1
3.132.886 7 17188025 18205037 gain Psg29,Ceacam5,Ceacam14,Gm5155,Ceacam11,Ceacam13,Ceacam12,Igfl3,Igfl3 9
591.926 7 26773232 26976928 gain Cyp2a5,Cyp2a5,Cyp2a5,Cyp2a22,Cyp2a22,Cyp2a22 6
4.170.656 7 20654493 24128503 gain Nlrp4e,Nlrp5,Gm10175,Zfp180,Zfp112 5
2.494.001 7 38898625 38991306 loss Gm21142,Gm25671 2
13.222.294 7 67330026 67943164 loss Mef2a,Lrrc28,Gm23233,Ttc23,Synm 5
1.330.269 7 7171339 10865583 loss Zfp418,Clcn4-2,Zik1,Nlrp4b 4
3.414.431 8 49942996 51497632 loss Gm23986 1
3.059.542 9 21959210 22072123 gain Epor,Rgl3,Ccdc151,Prkcsh,Elavl3,Zfp653 6
5.277.845 10 80335500 80575991 gain Reep6,Adamtsl5,Plk5,Mex3d,Mbd3,Uqcr11,Uqcr11,Tcf3,Gm25044,Gm25044,Gm25044,Gm25044,Onecut3,Atp8b3,Rexo1,Klf16 16
26.812.338 10 100597718 100692256 loss 1700017N19Rik 1
6.998.267 11 60393963 60504695 gain Lrrc48,Atpaf2,Gid4,Drg2,Myo15 5
2.624.723 11 75676344 76212635 gain Crk,Ywhae,Doc2b,Rph3al,1700016K19Rik,Fam101b,Vps53,Glod4,Fam57a,Gemin4 10
11.851.916 11 97742687 97853778 gain Pip4k2b,Cwc25,1700001P01Rik,Rpl23,SNORA21,Snora21,Lasp1 7
3.553.325 11 74899198 75121318 loss Tsr1,Srr,Smg6,Gm22733 4
309.751 11 105624215 107309569 loss Tanc2,Cyb561,Ace,Ace,Kcnh6,Dcaf7,Taco1,Map3k3,Limd2,Strada,Ccdc47,Ddx42,Ftsj3,Psmc5,Gm23645,Smarcd2,Tcam1,Gh,Gh,Gh,Gh,Gh,Cd79b,Scn4a,2310007L24Rik,Icam2,Ern1,Snord104,Gm22711,Tex2,Milr1,Gm25889,Polg2,Ddx5,Cep95,Smurf2,Bptf,Nol11,Pitpnc1 39
2.642.471 11 30118384 30155192 loss Sptbn1 1
10.304.184 12 114641806 116183315 gain Ighv1-73,Ighv1-83,Zfp386,Zfp386,Zfp386,Zfp386,Zfp386,Zfp386,Zfp386,Zfp386,Vipr2 11
1.414.343 12 116239354 117192837 loss Wdr60,Esyt2,Ncapg2,Gm25112,Gm24354,Ptprn2 6
2.875.469 14 10676764 10768859 loss Fhit 1
7.743.121 14 52237972 52331429 loss Rab2b,Gm23758,Tox4,Mettl3,Sall2 5
2.689.596 14 43932587 45325020 loss Ang5,Ang6,Ear2,Ear2,Ptgdr,Ptger2,Txndc16,Gpr137c,Ero1l 9
1.912.962 14 119385279 119496386 loss Hs6st3 1
950.029 14 118589508 118681878 loss Abcc4 1
4.105.345 14 3004822 8437757 loss Flnb,Dnase1l3,Abhd6,Rpp14,Rpp14,Pxk,Pdhb,Kctd6,Acox2,Fam107a,Oit1,4930452B06Rik 12
1.870.555 16 33446020 33668062 loss Zfp148,Slc12a8 2
3.148.258 17 5087550 8333690 gain Arid1b,Tmem242,Zdhhc14,Snx9,Synj2,Serac1,Gtf2h5,Tulp4,n-R5s26,Tmem181a,Dynlt1a,Dynlt1b,Tmem181b-ps,Tmem181b-ps,Dynlt1c,Tmem181c-ps,Dynlt1f,Sytl3,Ezr,Rsph3b,Tagap1,Rnaset2b,Rnaset2b,Gm25119,Rps6ka2,Ttll2,Gm9992,Gm26057,Fndc1,Tagap,Rsph3a,Gm22416,Rnaset2a,Rnaset2a,Fgfr1op,Ccr6,Mpc1,Sft2d1 38
50.819.398 17 40052632 40331607 gain Gm7148,Pgk2,Crisp3,Crisp1 4
4.099.936 17 14074943 15508274 loss Dact2,Smoc2,Thbs2,Gm23352,Wdr27,1600012H06Rik,Phf10,Gm3417,9030025P20Rik,Gm3448,Gm3435,Tcte3,Ermard,Dll1,Fam120b,Psmb1,Tbp 17
12.022.555 17 30590875 31053645 loss Glo1,Dnah8,Gm24661,Gm24661,Gm24661,Gm24661,Gm24661,Gm24661,Gm24661,Gm24661,Glp1r,Umodl1 12
5.135.466 17 36160573 36277761 loss Gm22453,Rpp21,Trim39 3
4.254.769 17 27372278 27593833 loss Grm4,Hmga1,Nudt3 3
5.565.997 18 87905985 87999255 loss Gm24987,Gm24987 2
df2 =
Recursive_level logp chr start end CNA Genes No.of.Gene
1 1.416.541 1 68580000 68640000 loss Erbb4 1
1 7.876.897 1 173840000 174010000 loss Mndal,Mnda,Ifi203,Ifi202b 4
1 6.280.751 1 173500000 173660000 loss BC094916,Pydc4,Pyhin1 3
1 7.369.317 1 115900000 116280000 loss Cntnap5a 1
2 128.766 2 146170000 146660000 gain 4930529M08Rik,Insm1,Ralgapa2 3
1 5.777.222 2 76720000 76800000 loss Ttn 1
2 1.448.913 3 15360000 16000000 loss Sirpb1a,Sirpb1a,Sirpb1a,Sirpb1a,Sirpb1b,Sirpb1b,Sirpb1b,Sirpb1b,Sirpb1c,Sirpb1c,Sirpb1c,Sirpb1c 12
1 3.845.977 4 119500000 125160000 gain AA415398,AA415398,AA415398,AA415398,AA415398,Foxj3,Guca2a,Guca2b,Hivep3,Edn2,Foxo6,Scmh1,Slfnl1,Ctps,Cited4,Kcnq4,Nfyc,Mir30c-1,Mir30e,Rims3,Exo5,Zfp69,Smap2,Col9a2,Zmpste24,Tmco2,Rlf,Ppt1,Cap1,Mfsd2a,Mycl,Trit1,Bmp8b,Bmp8b,Oxct2b,Ppie,Hpcal4,Nt5c1a,Heyl,Pabpc4,Bmp8a,Bmp8a,Oxct2a,Macf1,Ndufs5,Akirin1,Rhbdl2,Mycbp,Rragc,Pou3f1,Utp11l,Fhl3,Sf3a3,Inpp5b,Mtf1,n-R5s192,1110065P20Rik,Yrdc,Maneal,Cdca8,Rspo1,Gnl2,Dnali1,Snip1,Meaf6,Zc3h12a 66
1 1.446.699 4 73900000 74180000 gain Frmd3 1
1 2.262.305 4 72740000 72880000 gain Aldoart1 1
1 1.234.215 4 80820000 84340000 gain Tyrp1,Lurap1l,Mpdz,n-R5s187,Nfib,Zdhhc21,Cer1,Frem1,Ttc39b,Snapc3,Psip1,Ccdc171,Bnc2 13
1 123.671 4 108480000 108760000 gain Zcchc11,Prpf38a,Orc1,Cc2d1b,Zfyve9 5
1 1.418.261 4 139400000 147600000 loss Ubr4,Iffo2,Aldh4a1,Tas1r2,Pax7,Klhdc7a,Igsf21,Arhgef10l,Rcc2,Padi4,Padi3,Padi1,Padi2,Sdhb,Atp13a2,Mfap2,Crocc,Necap2,Spata21,Szrd1,Fbxo42,Rsg1,Arhgef19,Epha2,Fam131c,Clcnka,Clcnka,Clcnkb,Clcnkb,Hspb7,Zbtb17,Spen,Fblim1,Tmem82,Slc25a34,Plekhm2,Ddi2,Rsc1a1,Agmat,Dnajc16,Casp9,Cela2a,Cela2a,Ctrc,Efhd2,Fhad1,Tmem51,Kazn,Prdm2,Pdpn,Lrrc38,1700012P22Rik,Aadacl3,9430007A20Rik,Dhrs3,Vps13d,Tnfrsf1b,Tnfrsf8,Zfp600,Zfp600,Rex2 61
1 8.113.817 6 129740000 129800000 gain Klri2 1
1 15.569.108 6 41360000 41480000 loss Prss3,Prss3,Prss1,Prss1 4
1 2.037.683 6 63480000 63700000 loss Grid2 1
2 14.694 7 38260000 38280000 gain Pop4 1
1 14.946 7 35780000 38280000 gain Zfp507,Tshz3,Zfp536,Uri1,Ccne1,1600014C10Rik,Plekhf1,Pop4 8
1 7.192.011 7 47500000 47620000 loss Mrgpra2b,Mrgpra3 2
1 1.722.108 7 26000000 26200000 loss Cyp2b13,Cyp2b9 2
1 12.683.495 7 11350000 11680000 loss Zscan4f 1
1 1.360.954 10 80900000 81100000 gain Timm13,Lmnb2,Gadd45b,Gng7,Diras1,Slc39a3,Sgta,Thop1,Creb3l3 9
1 267.959 11 97880000 98000000 gain Fbxo47,Plxdc1,Arl5c 3
1 1.872.174 11 75860000 76420000 gain Rph3al,1700016K19Rik,Fam101b,Vps53,Glod4,Fam57a,Gemin4,Rnmtl1,Nxn,Timm22,Abr 11
1 2.811.352 12 113560000 114920000 gain Ighv14-3,Ighv13-1,Ighv13-1,Ighv13-1,Ighv13-1,Ighv13-1,Ighv6-4,Ighv6-4,Ighv6-4,Ighv6-4,Ighv6-4,Ighv6-5,Ighv6-5,Ighv6-5,Ighv6-5,Ighv6-5 16
1 1.979.667 12 115860000 115980000 loss Ighv1-83 1
1 2.098.521 12 17420000 21160000 loss Nol10,Odc1,Hpcal1,5730507C01Rik,Asap2 5
1 21.864.853 13 12580000 12650000 loss Ero1lb 1
1 3.233.185 13 61500000 62780000 loss Ctsm,Cts3,Zfp808 3
1 5.640.895 14 53540000 53780000 gain Trav12-2,Trav12-3,Trav13-2,Trav14-2,Trav15-2-dv6-2,Trav3-3,Trav9-4,Trav9-4,Trav9-4,Trav9-4,Trav4-4-dv10,Trav5-4,Trav6-7-dv9,Trav7-6,Trav7-6,Trav7-6,Trav16,Trav13-4-dv7,Trav14-3,Trav3-4 20
1 2.942.081 14 86300000 97240000 gain Diap3,Tdrd3,Rps3a2,Pcdh20,Pcdh9,Klhl1 6
1 4.662.806 14 9840000 9880000 loss Fhit 1
1 3.638.346 14 43740000 44640000 loss Ear1,Ear1,Ear10,Ear10,Ang5,Ang6,Ear2,Ear2 8
1 1.709.546 14 35320000 37400000 loss Grid1,n-R5s46,Ccser2,Rgr,Lrit1,Lrit2,Cdhr1,2610528A11Rik,Ghitm 9
2 3.387.282 14 84060000 85740000 loss Pcdh17 1
1 2.140.909 14 68280000 86300000 loss Adam7,Adamdec1,Adam28,Stc1,Nkx2-6,Nkx3-1,Slc25a37,Synb,Entpd4,SYNB,Loxl2,R3hcc1,Chmp7,Tnfrsf10b,Tnfrsf10b,Tnfrsf10b,Tnfrsf10b,Rhobtb2,Pebp4,Egr3,Bin3,Ccar2,9930012K11Rik,9930012K11Rik,Pdlim2,Sorbs3,Ppp3cc,Slc39a14,Piwil2,Polr3d,Mir320,Phyhip,Bmp1,Sftpc,Lgi3,Reep4,Hr,Nudt18,Fam160b2,Dmtn,Fgf17,Npm2,Xpo7,Dok2,Gfra2,Fndc3a,Cysltr2,Rcbtb2,Rb1,Lpar6,Itm2b,Med4,Nudt15,Sucla2,Htr2a,Esd,Lrch1,5031414D18Rik,Lrrc63,Lcp1,Cpb2,Zc3h13,Siah3,Spert,Cog3,Slc25a30,Tpt1,Snora31,Gtf2f2,Kctd4,Gpalpp1,Nufip1,Rps2-ps6,Tsc22d1,Serp2,Lacc1,Ccdc122,Enox1,n-R5s48,Dnajc15,Epsti1,Fam216b,Tnfsf11,Akap11,Dgkh,Vwa8,Zfp957,Rgcc,Naa16,Mtrf1,Kbtbd7,Zbtbd6,Wbp4,Elf1,Sugt1,Lect1,Pcdh8,Olfm4,Pcdh17 99
1 3.810.267 14 109680000 111240000 loss n-R5s50,Slitrk6 2
1 3.924.724 15 77460000 77560000 loss Apol10a,Apol10a,Apol10a,Apol10a,Apol11a,Apol11a,Apol11a,Apol11a,Apol7c 9
1 7.728.161 16 44780000 44920000 gain Cd200r1,Cd200r1,Cd200r4,Cd200r4,Cd200r2,Cd200r2 6
1 348.511 17 73500000 76640000 gain Galnt14,Ehd3,Xdh,Memo1,Dpy30,Spast,Slc30a6,Nlrc4,Yipf4,Birc6,Ttc27,Ltbp1,Rasgrp3,Fam98a 14
1 1.052.043 17 36120000 36540000 gain Rpp21,Trim39 2
1 1.325.386 17 90420000 90540000 loss Nrxn1 1
1 4.438.061 17 38300000 38360000 loss Olfr137,Olfr137 2
1 125.062 17 30380000 30920000 loss Btbd9,Glo1,Dnah8,Glp1r 4
1 2.998.359 19 13860000 13900000 gain Olfr1502 1
2 3.307.524 19 30910000 30970000 loss Prkg1 1
When i tried
df2[mapply(function(x, y) length(intersect(x,y))>0,
strsplit(df1$Gene, ','), strsplit(df2$Gene, ',')),]
i got out
logp chr start end CNA Genes No.of.genes
39 2.689.596 14 43932587 45325020 loss Ang5,Ang6,Ear2,Ear2,Ptgdr,Ptger2,Txndc16,Gpr137c,Ero1l 9
But i can find many rows with at least one common Gene
We could split up the "Genes" column in each datasets with strsplit, then compare the corresponding list elements with mapply, check if there is any intersect and use that index to subset the "df2"
df2[mapply(function(x,y) any(x %in% y),
strsplit(df1$Gene, ','), strsplit(df2$Gene, ",")),]
# chr start end Gene
#1 1179 3360 gain Recl,Bcl,Trim3,Pop4
#3 7180 9229 loss Sox1
#4 8159 8360 loss Sox1
#5 9154 10588 loss Pekg
Or use intersect and length
df2[mapply(function(x, y) length(intersect(x,y))>0,
strsplit(df1$Gene, ','), strsplit(df2$Gene, ',')),]
Update
If we need to find whether a single "Gene" of first dataset is found in any of the rows of second data (using the updated dataset)
df2[sapply(strsplit(df2$Gene, ','), function(x)
any(sapply(strsplit(df1$Gene, ','), function(y) any(x %in% y)))),]

Resources