How to replace multiple values with 0 based on values in another column - replace

I am trying to replace all values in a column with 0 if their value in another column is less than 7. How do I replace multiple values with 0 in one column if their value is 1-6 in another? I thought it might be something like this:
dataframe$column1[dataframe$column2 == '6', ‘5’, ‘4’, ‘3’, ‘2’, ‘1’] <- 0
Column 1 is where I would want the values to be replaced with 0 depending on the values in column 2. Is this correct? Thanks.

Related

How to add leading Zero's to make location number in Power BI so it returns 3 digit #

Loc1= FORMAT('Table'[LOC],"000") Any suggestion would be helpful? The original Column LOC is a text field with returns 3 characters with leading Zero's. I need it in numeric form to be able to join and use it in calculated columns.
Found a work around to fix my problem, I came up with a solution to keep Loc column as text and add concatenate column with Business Unit ID and Loc & convert concatenated col to numeric field so I can use for filtering and joining.

Ifelse() R using transmute_at(), multiple columns, same condition, different return value

I am wondering how I can automate the replacement of values in boolean columns (the values are 0 and 1) using transmute_at(). The replacement value depends on the column, but the condition is the same: if the value is 1, then it shall be replaced by a string value (which in this example should be a fruit name. For the real example I have a vector of strings similar to the strings of fruit names here).
Here is the dataframe. The columns were supposed to be different from each other, but I didn't know how to sample differently per column:
df <- data.frame(matrix(nrow=5,ncol=4,data=sample(0:1, 10, replace = T)))
Here is the fruit names. replacement[1] should replace all 1's for column X1, and replacement[2] replacing 1's in column X2, and so forth:
replacement <- sample(fruit,4)
I have tried to work out this line using dplyr in R. The real dataframe have more columns, so I do need to select the columns during mutation:
df %>%
transmute_at(vars(matches("X")), ~ifelse(. == 1, replacement, .))
I might have an idea that R doesn't understand which value to replace the 1's with when I'm feeding it a whole vector of values, though I don't know exactly how to specify it.
Two notes:
transmute_at has been superseded by across. If we didn't have per-column varying replacement, then this would be enough ... but we need to be a little more creative. I suggest a simple "lookup" based on the column name.
Because you are mixing classes (replacement is character but your columns are integer), ifelse may produce inconsistent results: if anything is replaced with the fruit, then the column will be character, but if nothing is replaced then the column will remain integer. The fix is to use as.character(.).
One problem is that if you don't do this, you won't know about the risk until something downstream complains because your column is not character as you expect. The use of dplyr::if_else (if already using dplyr) or data.table::fifelse (analogous, just fyi here) mandates that you be clearer about this, making sure that you do not put yourself in this risky position.
You define replacement as a vector of length 4, which I'm inferring is meant to align with the columns of the frame. However, since you use matches("X"), there is the potential (defensive programming) that you try to run this on a frame with a different number of columns. Even if you don't ... it's bad practice hard-coding magic constants in your code, I suggest you define replacement based on the number of matching columns.
set.seed(42)
replacement <- sample(stringr::fruit, sum(grepl("X", names(df))))
names(replacement) <- grep("X", names(df), value = TRUE)
replacement
# X1 X2 X3 X4
# "mango" "pomelo" "date" "satsuma"
df %>%
transmute(across(
matches("X"), ~ if_else(. == 1, replacement[cur_column()], as.character(.))
))
# X1 X2 X3 X4
# 1 0 pomelo 0 satsuma
# 2 0 0 0 0
# 3 mango 0 date 0
# 4 mango 0 date 0
# 5 0 pomelo 0 satsuma

Count Number of Consecutive Occurrence of values in Google sheets

This is the original data, all the data are of two kinds: red and black. And then, I want to study the occurrence of all the blocks. The result will be like this:
It means the first streak of red(from index 1 to 3) has a length of 3, and the second streak which is black(from index 4 to 5) has a length of 2...
I want to find out an elegant way to calculate it but in sheets, it's very hard. COUNTIF and ROWS all can't perfectly resolve this problem.
Do you have an elegant way?
try:
=ARRAYFORMULA(QUERY(REGEXREPLACE(QUERY({TEXT(
VLOOKUP(ROW(B2:B20), QUERY((B2:B20<>B1:B19)*ROW(B2:B20),
"where Col1 <>0"), 1, 1), "000000")&"×"&B2:B20},
"select Col1,count(Col1) group by Col1 label count(Col1)''")&"", "(.+×)", ),
"where Col1 is not null"))
Not sure it's elegant, but you could add two helper columns, the first column checks if the record has changed, and the second counts until the next change using a MATCH. Note you'd need an extra "TRUE" below the last record to catch the last streak. Then you can use FILTER to show the blocks and occurances.

value replacement in pandas in multiple coulmn

i want to replace all the numeric values by 1 except 0.
i am creating a matrix of zero and one. where zero is Nan and 1 is any number which is 1 or greater than one

Display each digit in a separate column in a row in Google Sheets

I have a column of data in binary values and I would like to split each digit of the number in the column into different cells across a row. How would I go about doing so? I saw the split function, but could not get it to work. https://support.google.com/docs/answer/3094136?
One of my example inputs:
1000111110100101111011110
1000110000100101000010000
try with this (you just change A2 to your cell):
=transpose(arrayformula(mid(A2,row(A1:offset(A1,len(A2),0)),1)))
For some rows (I limited text length with 30 char, you can change it):
=transpose(ARRAYFORMULA(mid(transpose(query(arrayformula(if(isnumber(A1:A)=true ,text(A1:A,"0"),A1:A)),"Select Col1 where Col1<>''")),row(A1:A30),1)))
try:
=ARRAYFORMULA(REGEXEXTRACT(A1:A, REPT("(.)", LEN(A1:A))))