How do I add a string/integer into an existing text file at a specific location?  

My sample text looks like below:

```txt
No, Color, Height, age
1, blue,70,
2, white,65,
3, brown,49,
4, purple,71,
5, grey,60,

```

My text file has 4 columns, three columns have text, how do I write to any row in the fourth column?  

If I want to write 12 to the second row, the updated file (sample.txt) should look like this:

```txt
No, Color, Height, age
1, blue,70,12
2, white,65,
3, brown,49,
4, purple,71,
5, grey,60,

```

I have tried this:

```py
with open("sample.txt",'r') as file:
    data =file.readlines()
data[1]. split(",") [3] = 1
with open ('sample.txt', 'w') as file:
  file.writelines(data)
with open ('sample.txt', 'r') as file:
    print (file. Read())

```

But it does not work. Your help is needed.
['https://Stackoverflow.com/questions/74616016', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/5255512/']
Here's the proper `case_when` syntax.

```
df <- data.frame(x = c("A", "B", "C", "D"))

library(dplyr)

df <- df %>%
  mutate(y = case_when(x %in% c("A", "D") ~ 1,
                       x %in% c("B", "C") ~ 0,
                       TRUE ~ NA_real_))
df
#>   x y
#> 1 A 1
#> 2 B 0
#> 3 C 0
#> 4 D 1

```
You're combining syntaxes in a way that makes sense in speech but not in code.
Generally you can't use `foo == "G" | "H"`. You need to use `foo == "G" | foo == "H"`, or the handy shorthand `foo %in% c("G", "H")`.

Similarly `x %in% df == "A"` doesn't make sense `x %in% df` makes sense. `df == "A"` makes sense. Putting them together `x %in% df == ...` does not make sense to R. (Okay, it does make sense to R, but not the same sense it does to you. R will use its Order of Operations which evaluates `x %in% df` first and gets a `result` from that, and then checks whether that `result == "A"`, which is not what you want.)

Inside a `dplyr` function like `mutate`, you don't need to keep specifying `df`. You pipe in `df` and now you just need to use the column `x`. `x %in% df` looks like you're testing whether the column `x` is in the data frame `df`, which you don't need to do. Instead use `x %in% c("A", "D")`. Aron's answer shows the full correct syntax, I hope this answer helps you understand why.