Combination of conditions

my attempts to copy again

df$test[(df$1st==(1:3) & df$2nd <= 4)] <- 1
df$test[(df$1st==(1:3) & df$2nd <= 5)] <- 2
df$test[(df$1st==(1:3) & df$2nd <= 6)] <- 3

lead to warning "longer object length is not a multiple of shorter object length"and lots of NAin df$test, although some rivers work correctly.
What am I missing? Any help was appreciated.

sv

+3
source share
3 answers

The problem is in this line:

df$1st==(1:3)

you can use %in%

df$1st %in% (1:3)

The warning concludes that you are comparing vectors of different lengths ( 1:3has a length of 3 and df$1sthas a length of "only you know that").

Also, I think you missed that your values ​​were overwritten: df$2nd <= 4also df$2nd <= 6, therefore, all 1 and 2 are overwritten by 3.

+5
source

, df$1st==(1:3), , , , , , . c(1,2,3) , , , df.

, df$1st 1 3, :

df$1st>=1 & df$1st<=3
+4

transform() . transform() , , . . :

set.seed(42)
df <- data.frame("first" = sample(1:5, 10e5, TRUE), "second" = sample(4:8, 10e5, TRUE))

df <- transform(df
    , test =      ifelse(first %in% 1:3 & second == 4, 1
            , ifelse(first %in% 1:3 & second == 5, 2
            , ifelse(first %in% 1:3 & second == 6, 3, NA)))
    )

Second, the column names 1stand 2ndare not syntactically valid column names. Take a look at make.names()for more details on what the correct column names are. When working with, data.frameyou can use / abuse the argument check.names. For instance:

> df <- data.frame("1st" = sample(1:5, 10e5, TRUE), "2nd" = sample(4:8, 10e5, TRUE), check.names = FALSE)
> colnames(df)
[1] "1st" "2nd"
> df <- data.frame("1st" = sample(1:5, 10e5, TRUE), "2nd" = sample(4:8, 10e5, TRUE), check.names = TRUE)
> colnames(df)
[1] "X1st" "X2nd"
+1
source

Source: https://habr.com/ru/post/1779987/


All Articles