How to Remove the Negative Values from a Data Frame in R

how to remove the negative values from a data frame in R

I want to add that it's not necessary to write a for loop, you can just set:

dframe[dframe < 0] <- NA

As dframe < 0 gives the logical indices TRUE where dframe is less than zero, and can be used to index dframe and replace TRUE values with NA.

@MrFlick explained the use of NA instead of NULL, and how to ignore NA values when calculating means of each row:

rowMeans(dframe, na.rm=TRUE) 

Edited to answer question re: rowMeans producing NaNs and how to remove:

NA is "not available" and is a missing value indicator, while NaN is "not a number" which can be produced when the result of an arithmetic operation can't be defined numerically, e.g. 0/0. I can't see your dframe values, but I would guess that this is the result of taking the row means when all row values are NA, while setting na.rm=TRUE. See the difference between mean(c(NA, NA, NA), na.rm=TRUE) vs. mean(c(NA, NA, NA), na.rm=FALSE). You can leave NaN or decide how to define row means when all row values are negative and have been replaced by NA.

To consider only non-NaN values, you can subset for not NaN using !is.nan, see this example:

mea <- c(2, 4, NaN, 6)
mea
# [1] 2 4 NaN 6
!is.nan(mea) # not NaN, output logical
# [1] TRUE TRUE FALSE TRUE
mea <- mea[!is.nan(mea)]
# [1] 2 4 6

Or you can replace NaN values with some desired value by setting mea[is.nan(mea)] <- ??

Removing negative values and one positive value from R dataframe

There should be a simpler solution to this but here is one way. Also created my own example since the one shared did not have sufficient data points to test

#Original vector
x <- c(1, 2, -2, 1, -1, -1, 2, 3, -4, 1, 4)
#Count the frequency of negative numbers, keeping all the unique numbers
vals <- table(factor(abs(x[x < 0]), levels = unique(abs(x))))
#Count the frequency of absolute value of original vector
vals1 <- table(abs(x))
#Subtract the frequencies between two vectors
new_val <- vals1 - (vals * 2 )
#Recreate the new vector
as.integer(rep(names(new_val), new_val))
#[1] 1 2 3

How can I remove zero and negative values in data frame?

In fact you can simply do

b[b<=0] <- NA

resulting in

> b
x Y
1 1 1
2 2 2
3 NA 3
4 NA 4
5 4 5
6 50 6
7 8 7
8 NA 8

Remove all rows which contain at least one negative value in R

Another base R option using rowSums + sign

subset(kosoyCorrected,rowSums(sign(kosoyCorrected))==ncol(kosoyCorrected))

giving

    BER1_EW    BER2_EW   BER3_EW   BER4_EW   BER5_EW    BER6_EW
1 7.0876132 7.09928796 7.0871944 6.9631594 7.0867343 7.09934523
2 4.5994509 3.89325300 4.1603601 4.8141982 4.0901617 4.34070903
4 0.1325316 0.09994992 0.1235644 0.1384925 0.2176045 0.09164854
6 0.1072044 0.11755171 0.0608681 0.1436152 0.1094949 0.13081894

Keeping Only Negative Values Across Multiple Columns in Data Frame

Try this base R solution:

#Create index
index <- which(names(df)=='State')
#Data
df$Var <- apply(df[,-index],1,function (x) length(which(x<0)))
#Filter
df2 <- df[df$Var!=0,]

Staff.Confirmed Residents.Confirmed Staff.Deaths Resident.Deaths Staff.Recovered Residents.Recovered State
4 0 -61 0 0 0 0 Arkansas
6 0 15 0 0 -1 1 Colorado

How can I extract negative values from an existing column into a new column

either use if_else:

sy2.1 %>% 
group_by(Vannstand2.cm) %>%
mutate(Vannstand2_neg = if_else(Vannstand2.cm < 0.0, 0, NA))

or with case_when

sy2.1 %>% 
group_by(Vannstand2.cm) %>%
mutate(Vannstand2_neg = case_when(Vannstand2.cm < 0.0 ~ 0))

Replace selected columns' negative values with 0s or NAs using R

You could use across in the function:

library(tidyverse)

replace_negatives <- function(data){
df <- data %>%
mutate(across(1, ~ ifelse(. < 0, 0, .)),
across(3, ~ ifelse(. < 0, NA, .)))
return(df)
}

replace_negatives(df)

Output

  x1 x2 x3
1 0 -1 NA
2 0 -1 NA
3 0 -1 0
4 0 -1 1
5 1 -1 2


Related Topics



Leave a reply



Submit