factors

How can I keep NA when I change levels

丶灬走出姿态 提交于 2019-12-02 07:13:39
问题 I build a vector of factors containing NA. my_vec <- factor(c(NA,"a","b"),exclude=NULL) levels(my_vec) # [1] "a" "b" NA I change one of those levels. levels(my_vec)[levels(my_vec) == "b"] <- "c" NA disappears. levels(my_vec) # [1] "a" "c" How can I keep it ? EDIT @rawr gave a nice solution that can work most of the time, it works for my previous specific example, but not for the one I'll show below @Hack-R had a pragmatic option using addNA, I could make it work with that but I'd rather a

How to find prime factors of a number in c++?

别来无恙 提交于 2019-12-02 02:59:55
问题 I am attempting project euler question number 3, and I don't get the desired result. My logic: List all the factors of the number 13195 and save them in an array. Check if each number in the array is a prime. If the number is found to be prime save it in an other array. display the contents of the second array. Hope it contains only prime factors. RESULT: The first array contains all the factors as expected, The second I think duplicates the first array or slips in some non-primes, Please

How to find prime factors of a number in c++?

﹥>﹥吖頭↗ 提交于 2019-12-02 02:41:36
I am attempting project euler question number 3, and I don't get the desired result. My logic: List all the factors of the number 13195 and save them in an array. Check if each number in the array is a prime. If the number is found to be prime save it in an other array. display the contents of the second array. Hope it contains only prime factors. RESULT: The first array contains all the factors as expected, The second I think duplicates the first array or slips in some non-primes, Please help! :) My code: #include <iostream> using namespace std; long int x,y=2; long int number=13195; long int

Decompose a number into 2 prime co-factors

杀马特。学长 韩版系。学妹 提交于 2019-12-01 21:37:31
One of the requirements for Telegram Authentication is decomposing a given number into 2 prime co-factors. In particular P*Q = N, where N < 2^63 How can we find the smaller prime co-factor, such that P < square_root(N) My Suggestions: 1) pre-compute primes from 3 to 2^31.5 , then test if N mod P = 0 2) Find an algorithm to test for primes (but we still have to test N mod P =0 ) Is there an algorithm for primes that is well suited to this case? Ugh! I just put this program in and then realized you had tagged your question C#. This is C++, a version of Pollard Rho I wrote a couple years ago and

How to find the most frequent values across several columns containing factors

爱⌒轻易说出口 提交于 2019-12-01 16:33:59
I am still relatively new to R, so apologies in advance if my question seems too basic. My problem is as follows: I have a data set containing several factor variables, which have the same categories. I need to find the category, which occurs most frequently for each observation across the factor variables. In case of ties an arbitrary value can be chosen, although it would be great if I can have more control over it. My data set contains over a hundred factors. However, the structure is something like that: id <- 1:3 var1 <- c("red","yellow","green") var2 <- c("red","yellow","green") var3 <-

How to find the most frequent values across several columns containing factors

删除回忆录丶 提交于 2019-12-01 15:16:33
问题 I am still relatively new to R, so apologies in advance if my question seems too basic. My problem is as follows: I have a data set containing several factor variables, which have the same categories. I need to find the category, which occurs most frequently for each observation across the factor variables. In case of ties an arbitrary value can be chosen, although it would be great if I can have more control over it. My data set contains over a hundred factors. However, the structure is

Consistent factor levels for same value over different datasets

孤街醉人 提交于 2019-12-01 00:30:29
I'm not sure if I completely understand how factors work. So please correct me in an easy to understand way if I'm wrong. I always assumed that when doing regressions and what not, R behind the scenes concerts categorical variables into integers, but this part was outside of my train of thought. It would use the categorical values in a training set and after building a model, check for the same categorical value in the test dataset. Whatever the underlying 'levels' were - didnt matter to me. However, I've been thinking more... and need clarification - especially if I'm doing this wrong on how

Consistent factor levels for same value over different datasets

百般思念 提交于 2019-11-30 19:32:48
问题 I'm not sure if I completely understand how factors work. So please correct me in an easy to understand way if I'm wrong. I always assumed that when doing regressions and what not, R behind the scenes concerts categorical variables into integers, but this part was outside of my train of thought. It would use the categorical values in a training set and after building a model, check for the same categorical value in the test dataset. Whatever the underlying 'levels' were - didnt matter to me.

Python Pandas: how to turn a DataFrame with “factors” into a design matrix for linear regression?

风流意气都作罢 提交于 2019-11-30 09:12:40
If memory servies me, in R there is a data type called factor which when used within a DataFrame can be automatically unpacked into the necessary columns of a regression design matrix. For example, a factor containing True/False/Maybe values would be transformed into: 1 0 0 0 1 0 or 0 0 1 for the purpose of using lower level regression code. Is there a way to achieve something similar using the pandas library? I see that there is some regression support within Pandas, but since I have my own customised regression routines I am really interested in the construction of the design matrix (a 2d

Getting Factors of a Number

时光总嘲笑我的痴心妄想 提交于 2019-11-30 07:16:58
I'm trying to refactor this algorithm to make it faster. What would be the first refactoring here for speed? public int GetHowManyFactors(int numberToCheck) { // we know 1 is a factor and the numberToCheck int factorCount = 2; // start from 2 as we know 1 is a factor, and less than as numberToCheck is a factor for (int i = 2; i < numberToCheck; i++) { if (numberToCheck % i == 0) factorCount++; } return factorCount; } The first optimization you could make is that you only need to check up to the square root of the number. This is because factors come in pairs where one is less than the square