user9292
user9292

Reputation: 1145

Remove the number after space

I have the following data set:

id <- c(1,2,3,4)
x1 <- c("1234564 0", "2345673 1", "6487591 0", "2345610 0")
mdata <- data.frame(id,x1)

For column x1, I need to remove either the 0 or 1 after the space. So the final data is:

id  x1
1   1234564
2   2345673
3   6487591
4   2345610

Upvotes: 0

Views: 34

Answers (1)

Pierre L
Pierre L

Reputation: 28441

mdata$x1 <- sub(" .*", "", mdata$x1)
  id      x1
1  1 1234564
2  2 2345673
3  3 6487591
4  4 2345610

The pattern matches and eliminates a space followed by any other characters.

Upvotes: 2

Related Questions