Reputation: 1
I'm sure this has been answered somewhere before but I'm afraid I don't know enough about R to frame this question correctly.
I currently have a dataset with survey data answers for many different questions. Most of the questions are character strings. I'm looking to change certain character strings within certain columns into numeric values so that I can plot them on a graph.
Specifically, my dataset is called lb2009. One column, p10st, asks a question with 3 possible answers. The answers are 3 different possible sentences. I want to change one sentence to make it equal 1, another to make it equal 2, and another to make it equal 3.
If you could spell this out for me as easily as possible, I would greatly appreciate it. Thanks for your help.
Upvotes: 0
Views: 2972
Reputation: 6104
if you open up R, this code will run properly
# look at the full example iris data set (it's pre-loaded in your R)
iris
# first six records
head( iris )
# convert the `Species` column to numeric, so you get 1, 2, 3
as.numeric( iris$Species )
# now actually store that result back on the data frame
iris$SpeciesCat <- as.numeric( iris$Species )
# plot your results
hist( iris$SpeciesCat )
Upvotes: 0
Reputation: 7130
For example,
ans = c("my ans1","my ans2","my ans3")
as.numeric(factor(ans))
## [1] 1 2 3
Note that most file input functions like read.table
, read.csv
have the option of treating strings as factors. So you can just convert them using as.numeric
.
Upvotes: 1
Reputation: 1492
How about this:
sent1 <- lb2009$p10st == 'My first sentence'
sent2 <- lb2009$p10st == 'My second sentence'
lb2009[sent1, ] <- 1
lb2009[sent2, ] <- 2
lb2009[!sent1 & !sent2, ] <- 3
This will get the row indices of the matching sentences for the first two sentences. It then sets the specific rows to the values 1 and 2. The last line sets the rows that are not sentence 1 and not sentence 2 to 3
Upvotes: 0