I have a dataset as below.
Because of its large amount of data, I uploaded it through the sparklyr
package, so I can use only pipe statements.
pos <- str_sub(csj$helpful,2)
neg1 <- str_sub(csj$helpful,4)
csj <- csj %>% mutate(neg=replace(helpful,stringr::str_sub(csj$helpful,4)==1,0))
csj <- csj %>% mutate(help=pos/neg)
csj
is.null(csj$helpful)
I want to make a column named 'help' which is 'the first number of helpful column/2nd number of helpful column'.
If the 2nd number is 0, I need to change the 2nd number to 1 and then divide it.
The data frame name is csj
.
But it doesn't work.
I'll be glad if someone can help me solve this problem.
After I followed @Sebastian Hoyos's advice but still I got this col1,col2,col3 as NAN as below picture. (But the example he gave me worked). How should I solve this problem?
enter image description here
+) After I tried without as.numeric
the part then I got this result.
> csj %>%
+ mutate(col1 = stringi::stri_extract_first_regex(csj$helpful, pattern = "[0-9]"),#extract first number
+ col2 = stringi::stri_extract_last_regex(csj$helpful, pattern = "[0-9]"),#extract second
+ col3 = ifelse(col2 == 0, 1, col2 ),#change 0s to 1
+ help = col1/col3) #divide row1 and 3
# Source: lazy query [?? x 12]
# Database: spark_connection
`_c0` reviewerID asin helpful length_of_review overall unixReviewTime category col1 col2 col3 help
<int> <chr> <chr> <chr> <dbl> <chr> <chr> <chr> <chr> <chr> <chr> <dbl>
1 0 A1KLRMWW2FWPL4 31887 [0, 0] 172 5 1297468800 Clothes_s~ "" "" NA NaN
2 1 A2G5TCU2WDFZ65 31887 [0, 0] 306 5 1358553600 Clothes_s~ "" "" NA NaN
3 2 A1RLQXYNCMWRWN 31887 [0, 0] 312 5 1357257600 Clothes_s~ "" "" NA NaN
4 3 A8U3FAMSJVHS5 31887 [0, 0] 405 5 1398556800 Clothes_s~ "" "" NA NaN
5 4 A3GEOILWLK86XM 31887 [0, 0] 453 5 1394841600 Clothes_s~ "" "" NA NaN
6 5 A27UF1MSF3DB2 31887 [0, 0] 375 4 1396224000 Clothes_s~ "" "" NA NaN
7 6 A16GFPNVF4Y816 31887 [0, 0] 334 5 1399075200 Clothes_s~ "" "" NA NaN
8 7 A2M2APVYIB2U6K 31887 [0, 0] 158 5 1356220800 Clothes_s~ "" "" NA NaN
9 8 A1NJ71X3YPQNQ9 31887 [0, 0] 96 4 1384041600 Clothes_s~ "" "" NA NaN
10 9 A3EERSWHAI6SO 31887 [7, 8] 532 5 1349568000 Clothes_s~ "" "" NA NaN
# ... with more rows
>