[Solved] Linear regresion of rectangular table against one set of values

One way is to spread your data table using Country.Name as key: dat.spread <- dat %>% spread(key=”Country.Name”, value=”inflation”) dat.spread %>% str ‘data.frame’: 50 obs. of 31 variables: $ year : chr “1967” “1968” “1969” “1970” … $ Albania : num NA NA NA NA NA NA NA NA NA NA … $ Armenia : num … Read more

[Solved] melting data.frame in R

Here’s a one-liner, base solution: out <- t(apply(mydf, 2, function(x) row.names(mydf)[which(as.logical(x))])) The result is a matrix: > out [,1] [,2] name1 “word1” “word3” name2 “word2” “word3” name3 “word1” “word2” which is easily made into a dataframe: > as.data.frame(out) V1 V2 name1 word1 word3 name2 word2 word3 name3 word1 word2 Here’s your data as I read … Read more

[Solved] Error in setting up and cleaning a dataframe R

Let’s simply read and think about the error message: Error: variable ‘dummygen’ was fitted with type “numeric” but type “factor” was supplied This error occurs after the line: ooslogit <- predict.glm(logit, newlogit, se.fit=TRUE) (Presumably, at least, because you’re question isn’t very clear about this and provides lots of code that doesn’t seem related.) So R … Read more

[Solved] How to use the variable from the python in rpy2?

First, make two lists from your database. Something like: cursor = con.cursor() cursor.execute(“SELECT * FROM traffic”) #Retrieves data from SQL rows = cursor.fetchall() Month = list() Traffic = list() for row in rows: Month.append(row[‘Month’]) # guesswork – what does a row look like? Traffic.append(row[‘Traffic’]) Then, now you have two python lists, you can make a … Read more

(Solved) Two by two matching between dataframes in r

You want the merge function. Since your column names that you want to match on already have the same name you don’t even need to do anything special. If that wasn’t the case you would want to look into the by.x and by.y parameters that merge takes. df1 = data.frame(Site.1=c(“A”,”A”,”B”),Site.2=c(“B”,”C”,”C”),Score1=c(60,70,80)) df2 = data.frame(Site.1=c(“B”,”A”,”A”),Site.2=c(“C”,”B”,”C”), Score2=c(10,20,30)) df3 … Read more

(Solved) How to make a great R reproducible example

Basically, a minimal reproducible example (MRE) should enable others to exactly reproduce your issue on their machines. Please do not post images of your data, code, or console output! tl;dr A MRE consists of the following items: a minimal dataset, necessary to demonstrate the problem the minimal runnable code necessary to reproduce the issue, which … Read more

[Solved] Date-time data in R

First convert your dates into a date-time class using asPOSIXct df = data.frame(x = c(“2012-03-01T00:05:55+00:00”, “2012-03-01T00:06:23+00:00”, “2012-03-01T00:06:52+00:00”)) df$times = as.POSIXct(df$x, format = “%Y-%m-%dT00:%H:%M+%S”) Then extract just the hour part using format df$hour = format(df$times, ‘%H’) This give you : x times hour 1 2012-03-01T00:05:55+00:00 2012-03-01 05:55:00 05 2 2012-03-01T00:06:23+00:00 2012-03-01 06:23:00 06 3 2012-03-01T00:06:52+00:00 2012-03-01 … Read more