Miguel123 Miguel123 - 23 days ago 8
R Question

R - change data frame entries by specific string (count and replace by the value of counter, change sign)

I have a data frame that contains numbers and numbers separated by a "." and I want to change the entries dependent on the "." string.
If the entry does not contain a "." the prefix "-" should be added. That's kind of simple using the subsetting or grep functionality. But I also want to replace the entries which contain a "." with the counter of ".".

my example data:

X1 X2
1 2
3 4
6 8
5 1.2
3.4 7
1.2.5 9
11 3.4.7


and I would like to have it look like this:

X1 X2
-1 -2
-3 -4
-6 -8
-5 1
2 -7
3 -9
-11 4


I have no clue and tried already subsetting, extracting the "." parts to count them. But I can not insert the counter. Thanks for your help.

Answer

Here it is with data.table The idea is to create a counter in an temporary column:

library(data.table)

dt<-data.table(df)
dt$X1 <- as.character(dt$X1 )
dt$X2 <- as.character(dt$X2 )
dt[!grepl(".", dt$X1, fixed=TRUE),X1:=paste("-", X1, sep="") ]
dt[!grepl(".", dt$X2, fixed=TRUE),X2:=paste("-", X2, sep="") ]
dt[grepl(".", dt$X1, fixed=TRUE)|grepl(".", dt$X2, fixed=TRUE), count_point:=as.character(sequence(.N))]
dt[grepl(".", dt$X1, fixed=TRUE),X1:=count_point]
dt[grepl(".", dt$X2, fixed=TRUE),X2:=count_point]
df <- data.frame(dt[, c("X1", "X2"), with = FALSE])

There should be a way to do it in less line, using .SD

Comments