Reputation: 111
So, I have an imported set of NBA data that I got using dataset <- read_csv(...)
. One of the columns named Experience
contains both integers and characters, so its type is set to character. I need to change every "R"
character to 0
's, then set the column type of Experience
to integer.
I'm just starting to use R, so I'm not quite sure what to do syntax-wise. Should I run a for all 'R' in "Experience"
kind of loop? I'd know how to do it in Python, but I'm not sure how to do it in R. Thanks.
Upvotes: 0
Views: 99
Reputation: 7659
The way you describe it, this should work:
dataset[ dataset$Experience == "R", "Experience" ] <- 0
does the replacement job in R
's vectorised manner:
dataset[ dataset$Experience == "R", ]
(the last comma matters) selects all rows in the data frame where the value of the column Experience
is equal to "R"
.
dataset[ , "Experience" ]
(again, the comma matters) selects the values in column "Experience"
.
Combined into one, exactly the values you are targeting are selected, and then the value 0
is assigned where the "R"
was.
You can find out more with the built-in help: ?"[.data.frame"
.
And then,
dataset$Experience <- as.integer( dataset$Experience )
takes care of the conversion.
Sample:
dataset <- structure(list(bla = 1:10, Experience = c("50", "49", "R", "47", "46", "R", "44", "R", "42", "41")), .Names = c("bla", "Experience"), row.names = c(NA, -10L), class = "data.frame")
dataset
bla Experience
1 1 50
2 2 49
3 3 R
4 4 47
5 5 46
6 6 R
7 7 44
8 8 R
9 9 42
10 10 41
dataset[ dataset$Experience == "R", "Experience" ] <- 0
dataset$Experience <- as.integer( dataset$Experience )
str( dataset )
'data.frame': 10 obs. of 2 variables:
$ bla : int 1 2 3 4 5 6 7 8 9 10
$ Experience: int 50 49 0 47 46 0 44 0 42 41
Upvotes: 1