Base R solution: reshape(mydf, timevar = "years", idvar= "MemberID", direction = "wide") MemberID a.Y1 b.Y1 c.Y1 d.Y1 a.Y2 b.Y2 c.Y2 d.Y2 1 123 0 0 1 0 0 1 0 0 3 234 1 0 0 0 0 0 1 0 Solution using reshape2 (and magrittr): mydf %>% melt(c('MemberID','years')) %>%...

Your question is not reproducible, so I'll illustrate on a stand alone code If you"ll run library(ggplot2) ggplot(mtcars, aes(factor(cyl))) + geom_bar() You'll get If you wish to add unused levels to factor(cyl) you can use scale_x_discrete using limits and drop = F. For Example ggplot(mtcars, aes(factor(cyl))) + geom_bar() + scale_x_discrete(limits...

r,duplicates,dataframes,levels,factors

Try load('adn.RData') indx <- sapply(adn, is.factor) adn[indx] <- lapply(adn[indx], function(x) { levels(x) <- make.unique(levels(x)) x }) table(adn[['adn01']], useNA='ifany') # Incorrect Incorrect.1 Partially correct Partially correct.1 # 5 3 1 3 # Correct <NA> # 2 1 table(adn[['adn03']], useNA='ifany') # Incorrect Partially correct Correct <NA> # 6 3 5 1 Update...

Saving as an R object solved my problem. Thanks, user20650.

May be you need df1 <- subset(df, specialty %in% c('Real Estate', 'Tort')) library(reshape2) dM <- melt(df1, id.var='specialty')[,-2] dM[] <- lapply(dM, factor) table(dM) # value #specialty 38564 44140 44950 49000 49255 49419 NULL # Real Estate 0 0 0 2 0 1 3 # Tort 1 1 1 1 2 0...

Look at this: while is_not_prime(y): prime_factors(y) 16 is not prime so you are stuck in an infinite while True loop here. If you want to break out of this loop, you are going to need to change y inside the loop somehow. ...

c++,sieve-of-eratosthenes,factors

The hard limit on the arrays is set probably because the problem demands so? If not then just bad code. Inside the inner loop, you are calculating the largest power of a prime that divides the number. Why? See point 3. The number of factors of a number n...

It is my guess that you need a combination of grep & replace. This may speed-up changing levels with similar syllables ("ko", "kok"). Data example code <- as.factor(c("kok","osa","ma","kes", "koko", "osa-aikainen", "osa/kes")) Add level levels(code) <- c(levels(code), "kokop") Replace all instances containing "kok" with "kokop" new.code <- replace(code, (grep ("kok", code)),...

actionscript-3,flash,primes,factorization,factors

Try this : function prime_factorization( n:int ): Array { var a:Array = [] ; for (var i:int = 2; i <= n / i; i++) { while (n % i == 0) { a.push(i) ; n = n / i ; } } if (n > 1) a.push(n) ; return...

r,matrix,dataframes,level,factors

You could change the levels of the dataset "df" to be in the same order by looping (lapply) and convert to factor again with the specified levels and assign it back to the corresponding columns. lvls <- c('PASS', 'WARN', 'FAIL') df[] <- lapply(df, factor, levels=lvls) str(df) # 'data.frame': 5 obs....

new[,2] is a factor, not a numeric vector. Transform it first new$MY_NEW_COLUMN <-as.numeric(as.character(new[,2])) * 5 ...

To avoid the "duplicate" warnings and also create an ordered factor over v3 (ordered by v2), you can do: df %>% mutate(v3 = factor(v3, ordered=TRUE, levels=unique(df[order(df$v2),"v3"][[1]]))) ...

algorithm,primes,enumeration,factors

We can merge streams of multiples, produced so there are no duplicates in the first place. Starting with the list [1], for each unique prime factor p we multiply the list iteratively by the prime factor k times (where k is the multiplicity of p), to get k new lists,...

javascript,html,arrays,primes,factors

In the special cases of 0 and 1, don't bother with the for loop, since you know what the correct results should be. So put the if outside the for: if (thenumber < 2) { array[0] = thenumber; } else { for (var i = 1; i <= thenumber; i++)...

Let p1, p2, ..., pn be the prime factorization of r. (Note that the same prime number can appear multiple times in the prime factorization, e.g. it's possible that p1=2 and p2=2.) Then the prime factorization of r2 is p12 p22 ... pn2. Let d be a divisor of r2....

You need to use lapply. do a str on "b" str(b) This will let you know you have a list of 2 of 2 data.frames. So you need to use lapply along with sapply, to preserve the data structure lapply(b, function(x) sapply(x, function(x) as.numeric(as.character(x)))) You have D/N in your factor,...

Consider the following: for (int i=sqrt(x); i>=1; --i) { if ( x % i == 0 ) { cout << " y = " << i << endl; cout << " z = " << x / i << endl; break; } } Will this serve the purpose? Can you...

r,numeric,data-type-conversion,factors

if train_new is a data.frame and you want to change the columns starting with "Q1...", try this: train_new[,grep(pattern="^Q1",colnames(train_new))] = lapply(train_new[,grep(pattern="^Q1",colnames(train_new))],as.integer) or if not every column you are converting is a factor (could be char or numeric): train_new[,grep(pattern="^Q1",colnames(train_new))] = lapply(train_new[,grep(pattern="^Q1",colnames(train_new))], function(x) { as.integer(as.factor(x)) } ) Applied on a general data.frame df:...

Your is_prime function seems to incorrectly reuse the variable n in these lines: n = int(n**0.5)+1 for i in range(2,n): if n % i == 0: You might consider using more descriptive names, such as factor_limit for example....

Solved it! while len(factors)>0: l = factors[0] w = factors[-1] print("The eight possibilites for the lenghth of", l, "and the width of", w, "are: ") print() first = (x + w,y + l) print ("The First Rectangle is the one which is constructed between the point", original_coordinate, "and", first) print...

You are encountering how ordinal factor variables are handled by regression functions and the default set of contrasts are orthogonal polynomial contrasts up to degree n-1, where n is the number of levels for that factor. It's not going to be very easy to interpret that result ... especially if...

r,date,line-breaks,data-type-conversion,factors

You could specify na.strings and stringsAsFactors=FALSE in the read.csv/read.table. (I changed the delimiter to , and saved the input data) stk <- read.csv('Akash.csv', header=TRUE, stringsAsFactors=FALSE, sep=",", na.strings="\\N") head(stk,3) # sku Stockout_start Stockout_End create_date #1 0BX-164463 <NA> 1/29/2015 11:35 1/29/2015 11:35 #2 0BX-164463 2/11/2015 18:01 <NA> 2/11/2015 18:01 #3 0BX-164464 <NA>...

You could use regex lookaround to extract the numbers library(stringr) str_extract_all(x, '(?<=\\[|,)\\d+(?=,)')[[1]] #[1] "1" "2" ...

A quick solution would be something like Res <- cbind(df[1], VALUE = factor(max.col(df[-1]), ordered = TRUE)) Res # Pre VALUE # 1 1 6 # 2 1 5 # 3 1 5 # 4 1 5 str(Res) # 'data.frame': 4 obs. of 2 variables: # $ Pre : int 1...