0

Below is the data set for reference X1

X1 <- data.frame(A = c(2,3,4,5,6,7),
                 B = c(0,0,0.23,1.7,2.45,84.5))  
#  A     B
#1 2  0.00
#2 3  0.00
#3 4  0.23
#4 5  1.70
#5 6  2.45
#6 7 84.50

Now i need to replace values greater than 0 as 1

#  A B
#1 2 0
#2 3 0
#3 4 1
#4 5 1
#5 6 1
#6 7 1
nghauran
  • 6,648
  • 2
  • 20
  • 29

2 Answers2

1

With:

X1$B <- as.integer(X1$B > 0)

The result:

> X1
  A B
1 2 0
2 3 0
3 4 1
4 5 1
5 6 1
6 7 1
h3rm4n
  • 4,126
  • 15
  • 21
0

So your data.frame is:

X1<-data.frame(A=2:7,B=c(0,0,0.23,1.7,2.45,84.5))

and you want to replace all the values greater than 0 in the column B with 1. Use can use the standard built-in sub-replace function of '[<-', i.e.:

X1[X1[,"B"]>0,"B"]<-1

i.e. check if the values in the column B are greater than zero and if true replace them with 1.

lohisoturi
  • 11
  • 4