[R] Help with a setting some values of a df vector to 0 but not all values
PIKAL Petr
petr@p|k@| @end|ng |rom prechez@@cz
Thu Apr 18 14:54:29 CEST 2019
Hi
seems to me simple
Sample data
> nozero <- c(1565, 1569, 1674, 415, 1564)
> test <- sample(c(1:10, nozero), 250, replace=T)
> test
[1] 10 2 6 4 415 5 9 1565 1569 2 10 1569 415 1569 3
[16] 4 9 10 1 1 5 10 1 3 10 9 1564 4 10 8
[31] 6 1674 10 2 9 415 1674 4 1674 1569 6 6 1565 6 5
...
change all except nozero to zero
> test[!test %in% nozero] <- 0
> test
[1] 0 0 0 0 415 0 0 1565 1569 0 0 1569 415 1569 0
[16] 0 0 0 0 0 0 0 0 0 0 0 1564 0 0 0
[31] 0 1674 0 0 0 415 1674 0 1674 1569 0 0 1565 0 0
...
Cheers
Petr
> -----Original Message-----
> From: R-help <r-help-bounces using r-project.org> On Behalf Of Bill Poling
> Sent: Thursday, April 18, 2019 2:39 PM
> To: r-help (r-help using r-project.org) <r-help using r-project.org>
> Subject: [R] Help with a setting some values of a df vector to 0 but not all
> values
>
> Good morning.
>
> #RStudio Version 1.1.456
> sessionInfo()
> #R version 3.5.3 (2019-03-11)
> #Platform: x86_64-w64-mingw32/x64 (64-bit) #Running under: Windows >= 8
> x64 (build 9200)
>
> I have a df column that looks like the below.
>
> #68 ID's including the 0 value
>
> I want to set all the values to 0 with the exception of 5 which are c(1565, 1569,
> 1674, 415, 1564))
>
> I realize its basic but the routine eludes me, I have googled and there are
> plenty of urls for setting to NA, etc.. but nothing I found, yet, where it is a
> subset routine excluding a few values.
>
> I expect the routine would be something like --
>
> set df1$ClaimManagerID = 0 where df$ClaimManagerID NOT IN
> c(1565,1569,1674,415,1564)
>
> str(df$ClaimManagerID)
> int [1:18015] 1558 0 1565 1565 1565 1565 1565 0 1565 1565 ...
>
> |ClaimManagerID | cnt| pct| cum_pct|
> |:-------------- |--------:|-------- -:|-------- -:|
> |1565 | 11412| 0.6334721| 0.6334721|
> |0 | 6120| 0.3397169| 0.9731890|
> |1569 | 162| 0.0089925| 0.9821815|
> |1674 | 25| 0.0013877| 0.9835692|
> |415 | 21| 0.0011657| 0.9847349|
> |1564 | 20| 0.0011102| 0.9858451|
> |234 | 19| 0.0010547| 0.9868998|
> |521 | 17| 0.0009437| 0.9878435|
>
> etc....= 68 ID's
>
> Thank you for any help.
>
> WHP
>
>
>
> Confidentiality Notice This message is sent from Zelis. ...{{dropped:13}}
>
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-
> guide.html
> and provide commented, minimal, self-contained, reproducible code.
Osobní údaje: Informace o zpracování a ochraně osobních údajů obchodních partnerů PRECHEZA a.s. jsou zveřejněny na: https://www.precheza.cz/zasady-ochrany-osobnich-udaju/ | Information about processing and protection of business partner’s personal data are available on website: https://www.precheza.cz/en/personal-data-protection-principles/
Důvěrnost: Tento e-mail a jakékoliv k němu připojené dokumenty jsou důvěrné a podléhají tomuto právně závaznému prohláąení o vyloučení odpovědnosti: https://www.precheza.cz/01-dovetek/ | This email and any documents attached to it may be confidential and are subject to the legally binding disclaimer: https://www.precheza.cz/en/01-disclaimer/
More information about the R-help
mailing list