logo
Tags down

shadow

Remove specific rows in R


By : Greg Baird
Date : October 17 2020, 08:10 PM
hope this fix your issue You should use grep, not grepl. When you use grep you get the row indexes that match the pattern, while with grepl you get a boolean vector. You could do:
code :
rowIndexes = grep(x = df$col1, pattern = "refer")

df = df[-c(rowIndexes, rowIndexes+1, rowIndexes+2),]
> df
          a   b  c   d  e
1     00100  44  5  69 fr
2     refer  34 35   7 df
3  thisalso  46 15 167 as
4   thistoo  46 15 167 as
5     00100  11  5  67 uu
6     00100 563 25  23 tt
7     00100  44  5  69 fr
8     refer  34 35   7 df
9  thisalso  46 15 167 as
10  thistoo  11  5  67 uu
11    00100 563 25  23 tt
12    00100  44  5  69 fr
13    refer  34 35   7 df
14 thisalso  46 15 167 as
15  thistoo  11  5  67 uu
16    00100 563 25  23 tt
17    00100 563 25  23 tt
18    00100 563 25  23 tt

> rowIndexes = grep(x = df$col1, pattern = "refer")
> df = df[-c(rowIndexes, rowIndexes+1, rowIndexes+2),]

> df

       a   b  c  d  e
1  00100  44  5 69 fr
5  00100  11  5 67 uu
6  00100 563 25 23 tt
7  00100  44  5 69 fr
11 00100 563 25 23 tt
12 00100  44  5 69 fr
16 00100 563 25 23 tt
17 00100 563 25 23 tt
18 00100 563 25 23 tt
rowIndexes = grep(x = df$col1, pattern = "refer")
N = 2
indexesToRemove = sapply(rowIndexes, function(x){ x + (0:N) })
df = df[-indexesToRemove, ]


Share : facebook icon twitter icon

remove specific rows based on comparing 2 rows if not a match in python


By : Aditya Saky
Date : March 29 2020, 07:55 AM
will be helpful for those in need I have 2 dataframes with 2 columns each, 1 column of both dataframes has the same values but not some of them does not match , Try this:
code :
df_1[df_1['ID_2'].isin(df_2.ID_1)]

Remove rows where datetime is a specific day between specific times


By : user1588621
Date : March 29 2020, 07:55 AM
wish help you to fix your issue Once you have converted the column to datetime, you can easily perform a conditional operation on it as per your logic:
code :
df["date"] = pd.to_datetime(df["date"], format="%Y.%m.%d %H:%M:%S")

df = df.loc[~((df["date"].dt.weekday_name == "Monday") # Excludes Mondays before 2am
            & (df["date"].dt.hour < 2)) 

          & ~((df["date"].dt.weekday_name == "Friday") # Excludes Fridays after 10pm
            & (df["date"].dt.hour >= 22))]

Remove rows with specific NA column


By : Shyam Swaroop Nigam
Date : March 29 2020, 07:55 AM
With these it helps I have the Following dataset where some entries (unique A) Don't have data in B and others that have sometimes. , We can use ave grouped by A and remove the groups that has all NAs
code :
df[!with(df, ave(is.na(B), A, FUN = all)), ]

#  A  B
#2 2 NA
#3 3 77
#5 2 81
library(dplyr)
df %>%
   group_by(A) %>%
   filter(!all(is.na(B)))

How to remove rows of a data frame when specific amount are not in specific columns?


By : user2856698
Date : March 29 2020, 07:55 AM
may help you . I would do this by first sorting, then performing an LEFT OUTER JOIN using merge with an indicator to determine which rows to keep. Example,
code :
u = df.loc[:, ['A', 'B']]
u.values.sort()     #  sort columns of `u` 
df2.values.sort()   #  sort columns of `df2`

df[u.merge(df2, how='left', indicator='ind').eval('ind == "both"').values]

   A  B    C    D
0  4  2  320  700
1  5  7  400  800
4  5  2  250  360
df[['A', 'B']] = np.sort(df[['A', 'B']])
df2[:] = np.sort(df2)

df.merge(df2, on=['A', 'B'])

   A  B    C    D
0  2  4  320  700
1  5  7  400  800
2  2  5  250  360

Fix remove Rows in R (remove rows with specific negative value in one column)


By : Canalegrande
Date : March 29 2020, 07:55 AM
will be helpful for those in need You can use is.na and combine your condition with | (or) to subset like:
code :
x  <- data.frame(a=1:4, b=c(0,-1,-2,NA))

x[is.na(x$b) | x$b != -2,]
#  a  b
#1 1  0
#2 2 -1
#4 4 NA

subset(x, is.na(b) | b != -2)
#1 1  0
#2 2 -1
#4 4 NA

#Or as @Cath suggested
subset(x, !b %in% (-2))
#  a  b
#1 1  0
#2 2 -1
#4 4 NA
Related Posts Related Posts :
  • In R, is growing a list just as inefficient as growing a vector?
  • Flexdashboard, rhandsontable: how to programmatically access user updated table?
  • Creating Summary Table from R Variables
  • Average over groups and include previous groups
  • R: data.table count rows on specific columns > 0
  • Transform (shuffle) just 2 Fields in a Dataframe
  • Issue with replacing string by match in R
  • (very) Simple quantstrat trading model using logistic regression
  • R - count maximum number of consecutive dates
  • Problems using tidyr separate on "|"
  • Default value when calling a function in a for loop
  • Finding values in a matrix from list of values in R
  • count 0's in a zoo (or dataframe) object
  • Finding the first non-zero year in data frame for multiple variables using tidyverse
  • ggplot2 - how to assign geom_text with arrow icon to second yaxis scale
  • regex fails with dollar sign
  • Drop first element of list of lists, condense list of lists? Too many elements?
  • R - how to apply output of ifelse(str_detect ...) to whole group
  • caret package confusion matrix define positive case with multiple classes
  • Generating a pairwise 'distance' matrix
  • Change all R columns names using a reference file
  • In R & dabestr, how do I get grouped differences correctly?
  • Exclude or set a unique color to the bottom triangle of a correlation matrix heatmap
  • r shiny observe function clears text input
  • Split column by multiple delimiters, keeping delimiters
  • How to random search in a specified grid in caret package?
  • merge 2 data frames in a loop for each column in one of them
  • how to edit the codes for the summary of R S4 Object?
  • Flatten JSON list into data frame
  • Filtering a dataset and making a ggplot
  • Align cells vertically to be at the bottom flextable
  • R speed up sapply
  • invalid subscript type 'list' Azure Machine Learning
  • Use rollapply with xts object and an anonymous defined function
  • Isolate data frames from a spreadsheet to create a list
  • Error in xts, as.POSIXct "'order.by' cannot contain 'NA', 'NaN', or 'Inf'"
  • Column splitting in R
  • number similar/duplicated rows in R
  • Count the number of times each value appears in a row dataframe r
  • how to vectorise my code in r using for loop?
  • A function to fill in a column with NA of the same type
  • Network flow balancing constraint in R
  • Adding main titles from list to graphs in for loop
  • create a matrix in Perl or R if data is provided in CSV file
  • Passing column names as string to with
  • R - filtering rows and summing
  • How to change the order of fill aesthetic in faceted ggplot?
  • Function to remove outliers by group from dataframe
  • How to find the difference of max & min values in one group in a variable in a dataframe
  • Convert unicode to a readable string
  • Wrong scale/difficult to interpret times on time series object using 'ts'
  • Joining three numeric columns without adding them in r
  • Is there any way to extract the names of columns from an excel sheet without actually loading the sheet into the RAM?
  • case_when() not working: Error in mutate_impl(.data, dots)
  • Hide boxes if input not suitable in Shiny
  • Make nodes as images in R with visNetwork lib
  • Re-shape status columns based on value in another column
  • Why does the plot size differ between docx and html in rmarkdown::render?
  • Reverse x-axis that contains categorical data and a lot of annotations
  • shadow
    Privacy Policy - Terms - Contact Us © voile276.org