Re: [R] Loop over columns of dataframe and change values condtionally

2021-09-02 Thread Rui Barradas

Hello,

In the particular case you have, to change to NA based on condition, use 
`is.na<-`.


Here is some test data, 3 times the same df.


set.seed(2021)
df3 <- df2 <- df1 <- data.frame(
  x = c(0, 0, 1, 2, 3),
  y = c(1, 2, 3, 0, 0),
  z = rbinom(5, 1, prob = c(0.25, 0.75)),
  a = letters[1:5]
)


# change all columns
is.na(df1) <- df1 == 0
df1

# only one column
is.na(df2[, 2]) <- df2[, 2] == 0
df2

# change several columns given by an index
is.na(df3[c(1, 3)]) <- df3[c(1, 3)] == 0
df3


Hope this helps,

Rui Barradas


Às 14:35 de 02/09/21, Luigi Marongiu escreveu:

Hello,
it is possible to select the columns of a dataframe in sequence with:
```
for(i in 1:ncol(df)) {
   df[ , i]
}
# or
for(i in 1:ncol(df)) {
   df[ i]
}
```
And change all values with, for instance:
```
for(i in 1:ncol(df)) {
   df[ , i] <- df[ , i] + 10
}
```
Is it possible to apply a condition? What would be the syntax?
For instance, to change all 0s in a column to NA would `df[i][df[i ==
0] = NA` be right?
Thank you




__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Loop over columns of dataframe and change values condtionally

2021-09-02 Thread PIKAL Petr
Hi

you could operate with whole data frame (sometimes)
head(iris)
  Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1  5.1 3.5  1.4 0.2  setosa
2  4.9 3.0  1.4 0.2  setosa
3  4.7 3.2  1.3 0.2  setosa
4  4.6 3.1  1.5 0.2  setosa
5  5.0 3.6  1.4 0.2  setosa
6  5.4 3.9  1.7 0.4  setosa

chenge all

> head(iris[,1:4]+10) 
  Sepal.Length Sepal.Width Petal.Length Petal.Width
1 15.113.5 11.410.2
2 14.913.0 11.410.2
3 14.713.2 11.310.2
4 14.613.1 11.510.2
5 15.013.6 11.410.2
6 15.413.9 11.710.4

change only some
> iris[,1:4][iris[,1:4]<2] <- iris[,1:4][iris[,1:4]<2]+10
> head(iris)
  Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1  5.1 3.5 11.410.2  setosa
2  4.9 3.0 11.410.2  setosa
3  4.7 3.2 11.310.2  setosa
4  4.6 3.1 11.510.2  setosa
5  5.0 3.6 11.410.2  setosa
6  5.4 3.9 11.710.4  setosa


Cheers
Petr


> -Original Message-
> From: R-help  On Behalf Of Luigi Marongiu
> Sent: Thursday, September 2, 2021 3:35 PM
> To: r-help 
> Subject: [R] Loop over columns of dataframe and change values condtionally
> 
> Hello,
> it is possible to select the columns of a dataframe in sequence with:
> ```
> for(i in 1:ncol(df)) {
>   df[ , i]
> }
> # or
> for(i in 1:ncol(df)) {
>   df[ i]
> }
> ```
> And change all values with, for instance:
> ```
> for(i in 1:ncol(df)) {
>   df[ , i] <- df[ , i] + 10
> }
> ```
> Is it possible to apply a condition? What would be the syntax?
> For instance, to change all 0s in a column to NA would `df[i][df[i == 0] =
NA`
> be right?
> Thank you
> 
> 
> --
> Best regards,
> Luigi
> 
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-
> guide.html
> and provide commented, minimal, self-contained, reproducible code.
__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] Loop over columns of dataframe and change values condtionally

2021-09-02 Thread Luigi Marongiu
Hello,
it is possible to select the columns of a dataframe in sequence with:
```
for(i in 1:ncol(df)) {
  df[ , i]
}
# or
for(i in 1:ncol(df)) {
  df[ i]
}
```
And change all values with, for instance:
```
for(i in 1:ncol(df)) {
  df[ , i] <- df[ , i] + 10
}
```
Is it possible to apply a condition? What would be the syntax?
For instance, to change all 0s in a column to NA would `df[i][df[i ==
0] = NA` be right?
Thank you


-- 
Best regards,
Luigi

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.