0

I have a list of 3 elements. I would like to remove rows that names are "Zfp644", "Fah" from the list in all elements. Please see the list below.

$vf1
                       m            v          vm
Snhg5          0.7297866  0.372041865  0.26794810
Clpx           0.6371744  0.235615154  0.22764196
Sh3glb2        0.4190627  0.151526394  0.13856866
Fah            1.0384479  0.502904971  0.41143590
Zfp644         1.8097207  0.892813901  0.82060572

$vf2
                       m            v          vm
Snhg5          0.7297866  0.372041865  0.26794810
Clpx           0.6371744  0.235615154  0.22764196
Sh3glb2        0.4190627  0.151526394  0.13856866
Fah            1.0384479  0.502904971  0.41143590
Zfp644         1.8097207  0.892813901  0.82060572

$ra
                       m           v          vm
Snhg5          0.7417275  0.35430376  0.27324337
Clpx           0.6579610  0.22782727  0.23656837
Sh3glb2        0.3892816  0.10448095  0.12711838
Fah            1.2093665  0.54312350  0.49631294
Zfp644         1.6625907  0.71552681  0.73763909

giegie
  • 463
  • 4
  • 11

3 Answers3

3

If your list is called list_df you can try :

result <- lapply(list_df, function(x) 
                 subset(x, !rownames(x) %in% c("Zfp644", "Fah")))
result

#$vf1
#                m         v        vm
#Snhg5   0.7297866 0.3720419 0.2679481
#Clpx    0.6371744 0.2356152 0.2276420
#Sh3glb2 0.4190627 0.1515264 0.1385687

#$vf2
#                m         v        vm
#Snhg5   0.7297866 0.3720419 0.2679481
#Clpx    0.6371744 0.2356152 0.2276420
#Sh3glb2 0.4190627 0.1515264 0.1385687

#$ra
#                m         v        vm
#Snhg5   0.7417275 0.3543038 0.2732434
#Clpx    0.6579610 0.2278273 0.2365684
#Sh3glb2 0.3892816 0.1044810 0.1271184
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
  • Im trying to implement the above but here My external list Im reading it as data-frame here is my code but it looks like it is not sub-setting `result <- lapply(all_csv, function(x) subset(x, !rownames(x) %in% gene))` ..my gene is a external dataframe where I have gens which i want to remove from all_csv..what am i doing wrong? – kcm Jul 11 '22 at 10:49
3

Using tidyverse

library(dplyr)
library(purrr)
map(lst_df, ~ 
                filter(.x, !row.names(.x) %in% c("Zfp644", "Fah")))
akrun
  • 874,273
  • 37
  • 540
  • 662
2

You can try setdiff over row.names to subset the desired rows, e.g.,

> lapply(vf, function(x) x[setdiff(row.names(x), c("Zfp644", "Fah")), ])
$vf1
                m         v        vm
Snhg5   0.7297866 0.3720419 0.2679481
Clpx    0.6371744 0.2356152 0.2276420
Sh3glb2 0.4190627 0.1515264 0.1385687

$vf2
                m         v        vm
Snhg5   0.7297866 0.3720419 0.2679481
Clpx    0.6371744 0.2356152 0.2276420
Sh3glb2 0.4190627 0.1515264 0.1385687

$vf3
                m         v        vm
Snhg5   0.7417275 0.3543038 0.2732434
Clpx    0.6579610 0.2278273 0.2365684
Sh3glb2 0.3892816 0.1044810 0.1271184

Data

> dput(vf)
list(vf1 = structure(list(m = c(0.7297866, 0.6371744, 0.4190627, 
1.0384479, 1.8097207), v = c(0.372041865, 0.235615154, 0.151526394,
0.502904971, 0.892813901), vm = c(0.2679481, 0.22764196, 0.13856866,
0.4114359, 0.82060572)), class = "data.frame", row.names = c("Snhg5",
"Clpx", "Sh3glb2", "Fah", "Zfp644")), vf2 = structure(list(m = c(0.7297866,
0.6371744, 0.4190627, 1.0384479, 1.8097207), v = c(0.372041865,
0.235615154, 0.151526394, 0.502904971, 0.892813901), vm = c(0.2679481,
0.22764196, 0.13856866, 0.4114359, 0.82060572)), class = "data.frame", row.names = c("Snhg5",   
"Clpx", "Sh3glb2", "Fah", "Zfp644")), vf3 = structure(list(m = c(0.7417275,
0.657961, 0.3892816, 1.2093665, 1.6625907), v = c(0.35430376,
0.22782727, 0.10448095, 0.5431235, 0.71552681), vm = c(0.27324337,
0.23656837, 0.12711838, 0.49631294, 0.73763909)), class = "data.frame", row.names = c("Snhg5", 
"Clpx", "Sh3glb2", "Fah", "Zfp644")))
ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81