-4

Consider a data set with a number of columns.

> data <- import.csv("data.csv")
> head(data[1])

> Output:

    Sex
1   M
2   M
3   F
4   M
5   I
6   I

(Isolating the first column of my data set.) I want to change the F to M in all cases.

So I try the following:

> for (i in 1:6) {
      if (data[i, 1] == F) {
          data[i, 1] = M) 
      }
  }

This leaves the data unchanged. So I tried to import the data like so:

> data <- import.csv("data.csv", stringsAsFactors = FALSE)

The loop method works now, but this is not OK because with the F, M, and I as strings I cannot perform the logistic regression that I need to.

Any suggestions on changing F to M but not introducing strings?

quanty
  • 824
  • 1
  • 12
  • 21

2 Answers2

0

Change all rows in data where Sex is equal to the string 'F':

data[data$Sex == 'F',] <- 'M'

You haven't told us what these variable types should be. Are they objects? Should they be of a certain type?

Sam
  • 644
  • 4
  • 21
  • Any type that is possible to use in a logistic regression (i.e. not strings) – quanty Nov 14 '17 at 10:17
  • Can you use factors? If so, just convert the input before you feed it into the model. `data$Sex <- as.factor(data$Sex)` – Sam Nov 14 '17 at 10:20
0

Try

Df$column <- ifelse(df$var== "F","M",df$var)