How to delete rows from a data.frame, based on an external list, using R?
Use %in%
:
main_data2 <- main_data[ ! main_data$NAMES %in% NAMES_list, ]
Delete rows that exist in another data frame?
You need the %in%
operator. So,
df1[!(df1$name %in% df2$name),]
should give you what you want.
df1$name %in% df2$name
tests whether the values indf1$name
are indf2$name
- The
!
operator reverses the result.
R: Removing rows from data frame based on external criteria
We can use anti_join
from dplyr
. df_final
is the final output.
library(dplyr)
df_final <- df.2 %>%
anti_join(df.1 %>% filter(is.na(feistiness)), by = c("date", "dog"))
Remove rows within elements of a list (in R)
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
How to remove rows from a data frame using a subset?
Data:
df <- data.frame(SFC = c("YU006UGD31092","YU006UGD31071",
"YU006UGD30152",
"YU006UGD25831",
"YU006UGD25831",
"YU006UGD25332" ,
"YU006UG922912",
"YU006UG922912"))
Code:
df %>%
group_by(SFC) %>%
filter(n() == 1)
Output:
SFC
<chr>
1 YU006UGD31092
2 YU006UGD31071
3 YU006UGD30152
4 YU006UGD25332
Edit:
If you have the list, you can also do:
df %>%
filter(!(SFC %in% Remove_SFC))
How to delete a particular value from the whole dataframe in R?
you can simply use df[df>=4] <- NA
to achieve what you want.
df <- data.frame(replicate(10,sample(0:10,10,rep=TRUE)))
> df
X1 X2 X3 X4 X5 X6 X7 X8 X9 X10
1 2 3 4 5 6 4 3 1 10 6
2 5 7 0 4 3 10 10 3 6 10
3 5 5 0 3 1 3 5 7 2 7
4 7 0 4 1 10 0 5 2 5 0
5 8 8 7 8 4 6 6 10 10 0
6 1 4 1 3 3 8 8 0 4 8
7 6 3 3 6 7 4 10 9 7 2
8 2 1 4 0 7 8 10 1 6 3
9 0 9 6 2 9 6 2 9 0 3
10 8 2 1 0 1 4 0 6 2 8
df[df>=4] <- NA
> df
X1 X2 X3 X4 X5 X6 X7 X8 X9 X10
1 2 3 NA NA NA NA 3 1 NA NA
2 NA NA 0 NA 3 NA NA 3 NA NA
3 NA NA 0 3 1 3 NA NA 2 NA
4 NA 0 NA 1 NA 0 NA 2 NA 0
5 NA NA NA NA NA NA NA NA NA 0
6 1 NA 1 3 3 NA NA 0 NA NA
7 NA 3 3 NA NA NA NA NA NA 2
8 2 1 NA 0 NA NA NA 1 NA 3
9 0 NA NA 2 NA NA 2 NA 0 3
10 NA 2 1 0 1 NA 0 NA 2 NA
Related Topics
Is There a Logical Way to Think About List Indexing
Can Dplyr Join on Multiple Columns or Composite Key
Finding Row Index Containing Maximum Value Using R
Add a Horizontal Line to Plot and Legend in Ggplot2
Stop an R Program Without Error
Generate Correlated Random Numbers from Binomial Distributions
Repeat Vector When Its Length Is Not a Multiple of Desired Total Length
Ggplot for Loop Outputs All the Same Graph
Add Percentage Labels to a Stacked Barplot
Stepwise Regression Using P-Values to Drop Variables with Nonsignificant P-Values
R Knitr Markdown: Output Plots Within for Loop
Object Not Found Error with Ddply Inside a Function
How to Add a Factor Column to Dataframe Based on a Conditional Statement from Another Column
Multiple Time Series in One Plot