Extracting specific value from a factor


#1

Given dataset:
wt =
[1] 152,821 155,707 159,443 160,053 163,741 164,760 164,131
[8] 167,405 168,672 171,287 172,307 175,223 178,692 179,006
[15] 182,528 183,740 185,733 190,345 193,160 197,016 198,741 ...
186 Levels: 152,821 155,707 159,443 160,053 ... 779,760

How do I extract, say the fourth value, and the first value in order for me to evaluate them? I tried using wt[4] but it returns
[1] 160,053
186 Levels: 152,821 155,707 159,443 160,053 ... 779,760
Thus, it is not meaningful for operations to be functioned on.
Say I can't even subtract the value, 160053 with 1.
Thanks.


#2

You will need to convert your factor values to numerical values:

library(tidyverse)
wt = wt %>% str_replace(',','') %>% as.numeric

Now you can do:

wt[4] - 1

#3

Just to add, note that factors in R represent categorical variables.

A nice discussion on the topic is here "Understanding factors"