r/R_Programming • • Jan 30 '18

A way to generate a sequence of numbers from a range of numbers?

2 Upvotes

Hey guys, First, I apologize for the mobile formatting. I have provided a more accurate depiction of what I need below. I have a large dataset that is essentially a list of ID numbers, and associated with each ID number is a range of numbers.

 ID start end
   1  1  50
   2  51  100
   3  101  150
   4  151  200
  etc etc

I was able to get something like this to work by ordering the numbers, then creating a new column with sequence

 df=test[rep(1:nrow(test), test$ID),] #created correct number of C#'s and rows
 df2$Numbesr=seq(1:200) 

However, I have some that are non-sequential, so the numbering is off if I do it this way. I'm looking for some sort of rep() that goes by the min() and max() of each range, if this makes sense.

What my data actually looks like:

 ID start end
  1  1  50
  2  100  150
 3  151  250
 4  300  400
  etc etc

Is there a way to have R extrapolate from a range to a sequence with the ID number repeating? Essentially a table (or list) with 100 rows, each number has its own row and ID number associated with it. To make it more fun, the values I have aren't all sequential... Thank you in advance!!


r/R_Programming • • Jan 30 '18

HTTR close connection

2 Upvotes

I am wondering is there anyway to close the connection after a GET? close the TCP connection?!?!


r/R_Programming • • Dec 24 '17

Interactive/animated graphs

2 Upvotes

So I am looking for good resource (or resources) that help create gifs or other kinds of animated graphs in R.

So I have created an interactive graph using ggplot2 and plotly, but I want to create gifs and cool animated graphs. I cant seem to find any good resource for it so I am hoping someone can help me. I understand some might say "just use [enter programming language of choice]" but I am trying to master R and this is a step on that path.

Thanks in advance.


r/R_Programming • • Dec 08 '17

Help! I know I'm missing something obvious

2 Upvotes

Simple java boolean in netbeans: int b = 3; System.out.print((b/2)!=(b%2));

Returns false. WHY? My own logic says 3/2 is NOT equal to 1. So it should return TRUE. What am i missing here??


r/R_Programming • • Nov 24 '17

How to retain specific parts of an element in a column and eliminate everything else in R?

2 Upvotes

I have a column containing the brand and model of cars. How to keep only the brand and remove the model? In the original dataset, the brand and the mode were separated by a space. So I tried this:

carprice$CarName=gsub(pattern = " *", replacement = "",carprice$CarName)

What happened was the space got eliminated and now the brand and the model names are concatenated. I am not able to even undo it. So what originally was "audi 100", now it has become "audi100". Now how can I extract just the brand names?


r/R_Programming • • Nov 16 '17

How to turn a repeat loop into a for loop?

2 Upvotes

Hello,

I'm trying to run code from this website: http://amunategui.github.io/dealing-with-large-files/

The idea is to take a CSV file of unknown length that is too big to fit in your RAM and chunk it. Then do work on the chunks.

The author provides a nice repeat loop that specifies a chunk size and repeats read.table()'s of nrow=chunkSize until it reaches the end. But I want to repurpose this code for foreach() parallelization, which requires a for loop.

How do I write a for loop to chunk a CSV without knowing a value for i?

Thank you


r/R_Programming • • Nov 04 '17

Need to compared two delegate excel sheets to confirm analytics values

2 Upvotes

So for work we do a lot quality assurance with our metrics and we have an excel sheet that our budgets are in and we also have our program with the budgets in them as well. We typically will go back and forth between the two screen comparing making sure the values are the same on both. We have the ability to export our software sheet to an excel file. What would be the best way to import and compare the two. True/false answers are fine , just need to know if any of the values aren’t equal.


r/R_Programming • • Oct 05 '17

Date conversion

2 Upvotes

I have a date variable but the data enters were told to input the dates in this format YYYYMMDD. Do you guys know of any statements that can convert this to DD/MM/YYYY?

I'm trying to find the difference between dates and can't use the difftime function YYYYMMDD between two times.


r/R_Programming • • Sep 30 '17

I created a guide for developing R Powered Custom Visuals for Power BI

2 Upvotes

http://rpubs.com/jpf5046/313759

It took me forever to find out how to develop R Powered Custom Visual, once I figured it out, I thought a guide would be helpful for others.

Let me know what you think -- sorta cross platform, but maybe some people will find it useful here!


r/R_Programming • • Sep 30 '17

R recognizing numbers as a categorical variable instead of numeric-how to fix?

2 Upvotes

Hi guys. I have a homework assignment for a stats class that has us using the the 'Swiss' data set on R. I need to show the distribution for education and describe it with appropriate statistics, but it seems as if its not an numeric variable in R.

Here is the code I used to try to create a histogram:

hist(swiss$education, xlab= "Portion of Population Educated (Percent)", ylab="Frequency", main="Distribution of Population Educated in Switzerland 1888", right=F, col="blue")

Error in hist.default(swiss$education, xlab = "Portion of Population Educated (Percent)", : 'x' must be numeric

I also tried to take the mean and standard deviation:

mean(swiss$education) sd(swiss$education)

and got >>NA for both.

Is there any way for me to convert this to a numeric variable? To my understanding, these are both percent values showing the percent of males that were in agriculture or educated.

Thanks!


r/R_Programming • • Sep 28 '17

Is there a way to find the average time between dates for customers?

2 Upvotes

In my data set I have Customer id, order id, order date and order value. I want to look at the average time between orders for each customer. Ideally creating a data from with the following information; customer id, time between orders and a bonus would be average price of order


r/R_Programming • • Sep 20 '17

Is there a way to host a Shiny app on my own server?

2 Upvotes

So, as the thread suggests... Is there such a way?

I have been searching for a while and i could only find sources for Shiny servers with limited uptime and so on.

I want to host some of my code so other people can access the app and see maps etc.


r/R_Programming • • Sep 15 '17

Save dataframe name as list from a loop in R

2 Upvotes

I am looking to save the output of the plots as a list which I can then call from do.call function and arrange it into a multiple plots. Can anyone help me? The code is below

for (i in seq(1:length(KPI_Table_list))){
  assign(paste("plot_",KPI_Table_list[i], sep=""),
         ggplot() + geom_line(data = Summary_1, aes_string(x = 
"Month_No", y = paste("m_avg_", KPI_Table_list[i], sep=""), 
group =1)) +geom_point()
  )
}

do.call("grid.arrange", c(length(KPI_Table_list), ncol=2))

r/R_Programming • • Aug 31 '17

Creating missing rows of data

2 Upvotes

I have a data frame that looks something like this

Char1Char2OccDateEvalDateValue1Value2
Aa2016-12-012016-12-0110030
Aa2016-12-012017-01-014025
Aa2016-12-012017-02-013020
Aa2016-12-012017-04-01105
Aa2016-12-012017-05-0142
Aa2016-12-012017-06-0102
Aa2016-12-012017-07-0105
Ab2017-01-012017-01-014025
Ab2017-01-012017-02-013020
Ab2017-01-012017-03-01105
Ab2017-01-012017-04-0142
Ab2017-01-012017-06-0102
Ab2017-01-012017-07-0105
...

I want to add rows so that for each combination of Char1, Char2, and OccDate, I have a row for every single month between OccDate and the last month in the data (so max(Eval Date)). I want to put 0s in the value1 and value2 fields for any of the added rows.

Thoughts?


r/R_Programming • • Aug 02 '17

Free introduction to R webinar, Thursday 8/10

Thumbnail
info.leveledu.com
2 Upvotes

r/R_Programming • • Jul 24 '17

Randomforest: how can i see what observations at each split?

2 Upvotes

I want to use a continuous variable as my target. How can i create an output that shows me where each observation falls on the actual final models tree?


r/R_Programming • • Jul 22 '17

imputing dataset...taking forever. missforest package

2 Upvotes

most of my rows are complete. i have 3 rows that are about 40% missing values.

I tried to run miss forest over it to impute the data, but it never gets past the 1st itereation. my dataset is 50k rows.

any other packages that would speed this up or can i speed up missforest?


r/R_Programming • • Jul 16 '17

Import multiple excel files with same headers

2 Upvotes

I am looking to import 900 excel files into one data frame. They all have the exact same headers and columns. What's the most efficient way to do this? Thanks!


r/R_Programming • • Jun 21 '17

R flexdashboard project - How much should I charge the client?

2 Upvotes

The job is to produce a customized report (HTML format) from data analysis (plots/tables).


r/R_Programming • • Mar 02 '17

Trouble formatting data for use with Phyloseq

2 Upvotes

I've been trying to use the guide found here as a template for importing my data to R for use in the Phyloseq package, but keep hitting roadblocks.

Here's some sample code from that link:

otumat = matrix(sample(1:100, 100, replace = TRUE), nrow = 10, ncol = 10)
otumat

rownames(otumat) <- paste0("OTU", 1:nrow(otumat))
colnames(otumat) <- paste0("Sample", 1:ncol(otumat))
otumat

Here's my attempt to generate an equivalent matrix:

#imported dataset biom_wo_tax_nosize
biom_wo_tax_matrix_unref <- as.matrix(biom_wo_tax_nosize)

#shifted matrix layout so rows had desired length
biom_wo_tax_matrix_rowgood <- biom_wo_tax_matrix_unref[,-1]

#ensured row names were properly labeled
rownames(biom_wo_tax_matrix_rowgood) <- biom_wo_tax_matrix_unref[,1]

#columns already labeled properly, this is a redundant step so the matrix label reflects that both rows and columns are set up as desired
biom_wo_tax_matrix_rowcolgood <- biom_wo_tax_matrix_rowgood

Up to his point, the two matrices strongly resemble each other, just one has example data and one has my actual data. Column names are samples, row names are OTUs.

Then, things get messy.

Sample code:

OTU = otu_table(otumat, taxa_are_rows = TRUE)

My code:

OTU_wo_tax <- otu_table(biom_wo_tax_matrix_rowcolgood, taxa_are_rows = TRUE)

Sample code gives a table: samples as column labels, OTUs as row labels (same as matrix setup). My code throws an error:

Error in validObject(.Object) : invalid class “otu_table” object: 
Non-numeric matrix provided as OTU table.
Abundance is expected to be numeric.

So, I tweak my matrix:

biom_wo_tax_numeric <- as.numeric(biom_wo_tax_matrix_rowcolgood)

biom_wo_tax_matrix <- as.matrix(biom_wo_tax_numeric)

biom_wo_tax_df <- as.data.frame(biom_wo_tax_matrix)

And retry the adapted example code:

OTU_wo_tax <- otu_table(biom_wo_tax_matrix, taxa_are_rows = TRUE)

Now my code gives a table-ish of two columns: sp1-sp510,000+ in column 1, various values 0-9 in column 2, no row or column labels listed.

Why is my data either throwing an error or being turned into a 2-column unlabeled table instead of maintaining its formatting? Is there another way I can configure this data to have the otu_table(...) command work?


r/R_Programming • • Feb 28 '17

Are there any R packages specifically related to churn/customer adoption?

2 Upvotes

I've googled and ive seen ways where people have created their own ways using other stat techniques. but, curious if there were specific packages or more useful packages then simply writing code to iterate over the transactional data to identify churn.


r/R_Programming • • Feb 07 '17

How can i create a fibonacci series by creating a function which takes the first number of the series and total elements of the series as inputs?

2 Upvotes

So if the input is say (4,7) Output is -> 3 5 8 13 21 34 55


r/R_Programming • • Jan 21 '17

Accessing Data from Github API using R. Anyone know of an interesting use case for github data?

Thumbnail
medium.com
2 Upvotes

r/R_Programming • • Jan 14 '17

Can Anybody Help me With Association Rules??

2 Upvotes

Hi,

I was just wondering if anyone could help me with twitter analysis project. I want to see if users who tweet about one thing also tweet about something else. I've used the TwittR package in R studio to download tweets containing keywords and then downloaded the timelines of those users in python. My supervisor said I should be using association rules analysis but I have zero idea how to structure my data for the apriori algorithm to work which is a list of tweets like so:

user_name,id,created_at,text exampleuser,814495243068313603,2016-12-29 15:36:13, 'MT @nixon1788: Obama and the Left are disgusting anti Semitic pukes! #WithdrawUNFunding'

Does anyone know if it is even possible with the data I have? Any help would be greatly appreciated!


r/R_Programming • • Jan 13 '17

Need help with some syntax

2 Upvotes

Hi, I'm a beginner to R and seem to be going wrong with some syntax error here:

alligator = data.frame(
     InLength =  c(3.87,3.61,4.33,3.43,3.81,3.83,3.46,3.76,3.50,4.19,3.78,3.71,3.73,3.78,4.50)
     InWeight = c(4.87,3.93,6.46,3.33,4.38,4.70,3.50,4.50,3.58,3.64,5.90,4.43,4.38,4.42,4.25)

)

And here is the error:

alligator = data.frame(
+ InLength = c(3.87,3.61,4.33,3.43,3.81,3.83,3.46,3.76,3.50,4.19,3.78,3.71,3.73,3.78,4.50)
+ InWeight = c(4.87,3.93,6.46,3.33,4.38,4.70,3.50,4.50,3.58,3.64,5.90,4.43,4.38,4.42,4.25)
Error: unexpected symbol in:
"InLength = c(3.87,3.61,4.33,3.43,3.81,3.83,3.46,3.76,3.50,4.19,3.78,3.71,3.73,3.78,4.50)
InWeight"
> )
Error: unexpected ')' in ")"

Can you help me as to where I am going wrong ?