Home > OS >  R - Making code more professional/efficient
R - Making code more professional/efficient

Time:11-27

This is a part of my code:

a <- data.frame(X1 = c(9, 9, 9, 8, 9, 9, 8, 9, 8, 7),
                   X2 = c(8, 8, 6, 8, 6, 8, 9, 8, 8, 8),
                   X3= c(-3, -3, -3, -3, -3, -3, -2, -1, -3, -3),
                   X4= c(-5, -7, -5, -7, -7, -7, -7, -7, -7, -5),
                   X5= c(1, 1, -1, 1, 1, 1, 1, 1, 1, 1),
                   X6= c(9, 11, 11, 11, 11, 11, 9, 11, 11, 10),
                   X7= c(7, 8, 8, 7, 8, 8, 8, 8, 8, 6),
                   X8= c(1, 0, 0, 1, 1, 1, 1, 1, 0, 0),
                   X9= c(25, 25, 25, 24, 25, 25, 24, 25, 25, 24))

cov=cov(a)
cov[5,1:5]<-0
cov[1:5,5]<-0
cov[5,5]<-1

cov

I try to write this part of the code in a more professional way:

cov[5,1:5]<-0
cov[1:5,5]<-0
cov[5,5]<-1

I tried to do something like:

cov[5,1:5] & cov[1:5,5]<-0

but it does not work

CodePudding user response:

cov[5,1:4] <- cov[1:4,5] <-0

cov[5,5] <- 1

CodePudding user response:

cov[5,1:5] <- cov[1:5,5] <-0

  •  Tags:  
  • r
  • Related