How to Convert Only Some Positive Numbers to Negative Numbers (Conditional Recoding)

How to convert only SOME positive numbers to negative numbers (conditional recoding)?

new.Freq <- with(mydata, ifelse(Var1 <= 1, -Freq, Freq))

How to recode negative values in a set of variables based on if conditional statement in R?

You can use

df[, 2:5] <- abs(df[, 2:5])

Converting negative numbers to positive numbers but keeping positive numbers unchanged

The last part

negated = (~x + 1) & sign;

is wrong, you are going to get either 1 or 0, you have to create a mask with all
first 31 bits to 0 and only the last one to either 0 or 1.

Assuming that for you target you are dealing with 32 bit integers with 2
complement, you can do this:

#include <stdio.h>

// assuming 32bit, 2 complement
int sign_inverse(int n)
{
int mask = ~n & 0x80000000U;

if(n == 0)
mask = 0;

return (~n + 1) | mask;
}

int main(void)
{
int a = 5;
int b = -4;
int c = 54;
int d = 0;

printf("sign_inverse(%d) = %d\n", a, sign_inverse(a));
printf("sign_inverse(%d) = %d\n", b, sign_inverse(b));
printf("sign_inverse(%d) = %d\n", c, sign_inverse(c));
printf("sign_inverse(%d) = %d\n", d, sign_inverse(d));

return 0;
}

but you need at least 1 if for the case of 0, because the mask for 0 is 0x80000000.

The output of this is:

$ ./b 
sign_inverse(5) = -5
sign_inverse(-4) = 4
sign_inverse(54) = -54
sign_inverse(0) = 0

IF statement to turn positive values into negative values in Excel

If longitude data is in column C (from C2 onwards, say) - then you can deploy a couple of synonymous techniques - depending upon your preference:

  1. =if(C2>0,-C2,C2) : perhaps most intuitive
  2. =-abs(C2) : parsimonious, kudos to Jadefalke (26/01/22 - commentary)

Array functions exist for these too - i.e. for a column of data C2:C10, you can enter (once) in the corresponding leading (top) cell =-abs(C2:C10). see here for details RE: Office 365 compatible vs. prior versions of Excel.

How to convert positive numbers to negative in Python?

If you want to force a number to negative, regardless of whether it's initially positive or negative, you can use:

    -abs(n)

Note that integer 0 will remain 0.

Count number of negative values based on a condition of another variable in R

base R

Using by:

by(df$dummy, df$value > 0, FUN = sum)

# df$value > 0: FALSE
# [1] 3
# ---------------------------------------------------------------------
# df$value > 0: TRUE
# [1] 2

Using aggregate:

aggregate(dummy ~ value > 0, data = df, sum)

# value > 0 dummy
# 1 FALSE 3
# 2 TRUE 2


dplyr

library(dplyr)
df %>%
group_by(sign = value > 0) %>%
summarise(sum = sum(dummy))

# A tibble: 2 x 2
# sign sum
# <lgl> <int>
# 1 FALSE 3
# 2 TRUE 2

Or, using sign:

df %>% 
group_by(sign = sign(value)) %>%
summarise(sum = sum(dummy))

# A tibble: 3 x 2
sign sum
<dbl> <int>
1 -1 3
2 0 0
3 1 2


df <- structure(list(dummy = c(TRUE, FALSE, FALSE, FALSE, TRUE, TRUE, 
TRUE, FALSE, TRUE), value = c(-2, 3, 4, 2, -4.5, 9, -1, 0, 3)), class = "data.frame", row.names = c(NA,
-9L))

Convert a positive number to negative in C#

How about

myInt = myInt * -1

Excel custom formatting positive/negative numbers with Thousand/Million/Billion (K/M/B) suffixes

Since it appears to not be possible with a one-stop solution (which while I think doing this without a one-stop is a little messy, but I also understand why they can't just magically understand every conceivable custom format iteration), I am opting for a two-step approach:

I will have 3 custom formats. One for the positive numbers with suffixes, another for the negative numbers with suffixes, and a third that is just the "standard" positive/negative number format (displayed in the question). I will then use a series of two or three conditional formatting rules to determine which of these custom formats will be displayed.

Personally, I am going to use the +/- format as the cell's format, then apply two conditional rules that change it to the two suffix variations, but I could see the argument for using conditional formats for all three.

Thanks for the feedback and the reminder that conditional formatting exists to aid with this very kind of issue.



Related Topics



Leave a reply



Submit