Question

I know how to do some transformations to the table I read from file:

data = read.table(file, sep=" ", header=TRUE, stringsAsFactors=F)
data_transformed=cbind(data[, 1], round(
        data[, seq(from = 3, to = ncol(data), by = 3)] *100 /
        data[, seq(from = 2, to = ncol(data), by = 3)], digits=2))

And how to replace "Inf" by "0".

data_transformed[data_transformed==Inf] <- 0

Now, I need every number greater than 100 replace by (value-2*(value-100)) so that 130 will be replaced by 70 and 110 will be replaced by 90.

I am using following code, which does not work:

data_transformed[data_transformed>100] <- (data_transformed-2*(data_transformed-100))

Any suggestion how to fix it? Thank you very much.

I am getting this error:

Error in rowMeans(data_transformed, na.rm = TRUE) : 'x' must be numeric
Was it helpful?

Solution

I believe you have copied the wrong error statement. It is very possible to get this warning:

Warning message:
In data_transformed[data_transformed > 100] <- data_transformed -  :
  number of items to replace is not a multiple of replacement length

As it is mentioned in the comment by @IShouldBuyABoat. You have a filter data_transformed > 100 on the LHS, but you do not have a filter on the RHS. See this code:

set.seed(1)
data <- matrix(round(rbinom(210, 3, .5) * 100), ncol = 21)
head(data)

data_transformed <- cbind(data[, 1],
  round(data[, seq(from = 3, to = ncol(data), by = 3)] * 100 /
        data[, seq(from = 2, to = ncol(data), by = 3)], digits = 2))
head(data_transformed)

data_transformed[data_transformed == Inf] <- 0
head(data_transformed)

a <- data_transformed > 100
a

# data_transformed[data_transformed > 100] <-
#   data_transformed - 2 * (data_transformed - 100)

data_transformed[a] <-
  data_transformed[a] - 2 * (data_transformed[a] - 100)

head(data_transformed)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top