r/R_Programming • • Oct 30 '15

R programming exercises

2 Upvotes

I'm looking for programming exercises that deal with data manipulation. Maybe something similar to coding bat? I've used this for the Java programming and am looking for something similar. Open to other suggestions as well


r/R_Programming • • Oct 13 '15

Optimization in R? What is the mainstream package used?

2 Upvotes

I have heard of lpsolve. Are there any other open source alternatives?


r/R_Programming • • Sep 19 '15

Washington Post's 'Police Killings' Database

2 Upvotes

x <- read.csv("https://raw.githubusercontent.com/washingtonpost /data-police-shootings/master/fatal-police-shootings-data.csv")

Races <- c("Unknown", "Asian", "Black", "Hispanic", "Native American", "Other", "White")

as(levels(x$race), "character") <- c("Unknown", "Asian", "Black", "Hispanic", "Native American", "Other", "White")

hist(x = as.numeric(x$race), breaks = c(0:7), main = "Shot and Killed by Race", xlab = "Race", ylab = "Freq", labels = levels(x$race))

Note histogram of people shot and killed by race

Below are people shot and killed by race as a percent of the total number shot and killed

as of the last update to the Washington Post's "Police Killings" database.

percentBlack <- round((length(c(which(x$race == "Black")))/length(x$race))*100, digits = 2) percentWhite <- round((length(c(which(x$race == "White")))/length(x$race))*100, digits = 2) percentHispanic <- round((length(c(which(x$race == "Hispanic")))/length(x$race))*100, digits = 2) percentNative <- round((length(c(which(x$race == "Native American")))/length(x$race))*100, digits = 2) percentAsian <- round((length(c(which(x$race == "Asian")))/length(x$race))*100, digits = 2)

print("Black people shot and killed as a percent of total shot and killed"); print(paste(percentBlack, "%", sep = ""))

print("White people shot and killed as a percent of total shot and killed"); print(paste(percentWhite, "%", sep = ""))

print("Hispanic people shot and killed as a percent of total shot and killed"); print(paste(percentHispanic, "%", sep = ""))

print("Native American people shot and killed as a percent of total shot and killed"); print(paste(percentNative, "%", sep = ""))

print("Asian people shot and killed as a percent of total shot and killed"); print(paste(percentAsian, "%", sep = ""))

Note: Race categories "unknown" & "other" are not considered here so do not be alarmed that

the sum of the percentages above is less than 100.

General summary stats

totUnarmed <- (length(which(x$armed == "unarmed")) / length(x$armed))*100 totUnarmed <- paste(round(totUnarmed, digits = 2), "%", sep = "")

totVeh <- (length(which(x$armed == "vehicle")) / length(x$armed))*100 totVeh <- paste(round(totVeh, digits = 2), "%", sep = "")

totToy <- (length(which(x$armed == "toy weapon")) / length(x$armed))*100 totToy <- paste(round(totToy, digits = 2), "%", sep = "")

totGun <- (length(which(x$armed == "gun")) / length(x$armed))*100 totGun <- paste(round(totGun, digits = 2), "%", sep = "")

totGE <- (length(which(x$armed == "guns and explosives")) / length(x$armed))*100 totGE <- paste(round(totGE, digits = 2), "%", sep = "")

General Stats results

print("Total number unarmed as a percent of total shot and killed"); print(totUnarmed)

print("Total number armed with vehicle as a percent of total shot and killed"); print(totVeh)

print("Total number armed with toy weapon as a percent of total shot and killed"); print(totToy)

print("Total number armed with gun as a percent of total shot and killed"); print(totGun)

print("Total number armed with gun and explosives as a percent of total shot and killed"); print(totGE)

a start! Take it further!!


r/R_Programming • • Aug 15 '10

useR! 2010: Invited Lectures [videos]

Thumbnail user2010.org
2 Upvotes

r/R_Programming • • Aug 10 '10

Drew Conway's site on datamining the WikiLeaks data using R.

Thumbnail
drewconway.com
2 Upvotes

r/R_Programming • • Aug 10 '10

WikiLeaks Analysis in R

Thumbnail
github.com
2 Upvotes

r/R_Programming • • May 25 '25

how to customize piedonut chart of webr package

1 Upvotes

can anyone help me how i can fix the overlapping labels? i tried decreasing the text sieze, but they still overlap. im using the piedonut from the webr package


r/R_Programming • • Aug 19 '18

rvest html_node returns empty list

1 Upvotes

Dear all,

I was trying to scrape a webpage using the R package rvest but when I use the command html_node it returns an empty list. What is the problem? This is my code (I used SelectorGadget to obtain the tags):

library(tidyverse) library(rvest)

main.link<- "https://www.sreality.cz/en/search/for-sale/apartments/praha"

main.page<-read_html(main.link)

links<- html_nodes(main.page, css=".title .ng-binding")

As you can see I am a beginner in R. Thank you in advance for your help.


r/R_Programming • • Jul 04 '18

How to insert a csv data frame into an existing excel file in RStudio

1 Upvotes

Hi all,

I would like to copy a data frame from my .csv file into an xlsx template file. I am using this code:

#here, I have opened the csv file of interest and selected the    dataframe I want to copy into the template
data <-read.csv("153L_GONOGO.csv", header = T)
datatoexport <- data[,1:6]
datatoexport <- as.data.frame(data[,1:6])

#I have loaded my xlsx template file
wb <- loadWorkbook("GNG template.xlsx")
sheets <- getSheets(wb)
sheets

#I want to copy my csv data frame into sheet 1 of my xlsx template starting from row 2 and column 1 
addDataFrame(datatoexport, sheets$Sheet1, row.names = F, col.names = F, startRow = 2, startColumn = 1)
saveWorkbook(wb, file = "153L_Gonogoprova.xlsx")

This code has worked perfectly file with excel files previously and it is the first time I am using on a csv file. The error that I get is "Error in addDataFrame(datatoexport, sheets$Sheet1, row.names = F, col.names = F, : attempt to apply non-function". Probably the "addDataFrame" is not the right command I need to use but I don't what else could work. Thank you for your help.


r/R_Programming • • Jul 03 '18

Find the reverse direction edge and subtract its weight from the first's one.

1 Upvotes

Hello. I'm having a matrix like the following one

m <- expand.grid(LETTERS[1:24],LETTERS[1:24])
m$weight <- runif(nrow(m), 0.01, max = 1)
m <- m[m$Var1!=m$Var2, ] ##remove loop edges
colnames(m) = c("to","from","weight")

and in this form it describes a directed graph. What I want to do is to sabtract and take the absolute value of each pair of inverse edges and create a new matrix describing a new undirected graph.

i.e abs( edge_weight(A,B) - edge_weight(B,A) )

Any idea on how to achieve this? Thanks.


r/R_Programming • • May 12 '18

Shiny or Python’s Tkinter?

1 Upvotes

Hi,

I am intermediate skill level using shiny. I am considering learning python’s Tkinter as a way of mastering the FrontEnd space from a data analysts perspective.

Is it worth it? Is the shiny ecosystem sufficient enough? Or should I learn bootstrap instead?

Your thoughts are welcome


r/R_Programming • • Apr 27 '18

leaflet and ggplot code not rendering on document when using rmarkdown

1 Upvotes

Hi,

I am at the moment using rmarkdown to render to an MS Word document. I noticed that ggplot or leaflet code does not render on to my document when i knitr it. Generic R code renders but none involving leaflet or ggplot.

I am the latest version of R studio. My OS is Ubuntu 17.10.

If you could give me pointers I would really appreciate it.


r/R_Programming • • Apr 23 '18

Satterthwaite Approximation in Relation to LME4 and LMERTEST

1 Upvotes

So I'm working on a presentation where I'm explaining the lme4 package, and I've come to a bit of a roadblock. I'm using the lmerTest in order to generate p-values, and from my understanding of things, the Satterthwaite Approximation is used to generate them. For the life of me though, I can't figure out the reason why.

Can some one explain this to me like I was 5?


r/R_Programming • • Apr 09 '18

Programming Community Curated Resources For Learning R Programming (FREE)

Thumbnail
hackr.io
1 Upvotes

r/R_Programming • • Feb 28 '18

Equation behind power.t.test

1 Upvotes

Hey Folks,

Does anyone offhand know the equation behind the power.t.test calculation?

Thanks!


r/R_Programming • • Feb 12 '18

Data analysis, problem with string

1 Upvotes

I'm scrapping data and when I want to srap meterage of flat I get string. And I want to change it into numeric, Example:

metraz <- read_html("https://www.otodom.pl/oferta/zamieszkaj-w-apartamentowcu-przy-stacji-metra-ID3xMKL.html#gallery[1]") %>% html_node(".param_m strong") %>% html_text() %>% gsub(",",".", .) %>% gsub(" m²","", .)

But there is a problem, string contains for example "54,1 m²" and when I want to remove " m²" it doesn't want to do it. I think that R cannot recognise "²". What can I do?


r/R_Programming • • Feb 01 '18

using randomForest() with matrix() as input results to an Error: protect(): protection stack overflow

1 Upvotes

Hello,

I'm trying to figure out a solution online but couldn't so far. I realized that others dealt with such an error but their suggestions didn't work for me.

So, I'm trying to run the randomForest() using this command:

rf = randomForest(classes~., data=as.matrix(train), mtry=5, ntree=2000, importance=TRUE)

and it always results in Error: protect(): protection stack overflow.

As you can see, I have already turned the training dataset into a matrix but this didn't fix anything.

Cstack_info() size current direction eval_depth 7969177 13104 1 2

I also tried the options(expressions = 12e4) but nothing really changed.

The PC I'm running this on has 12 GB RAM and runs on Linux.

The dim(train) returns: 50 20040.

Is there something else to try, or I should run it on a different PC?

Thanks.


r/R_Programming • • Jan 27 '18

Auto update Shiny App

1 Upvotes

Has anyone figured out how to have a Shiny App update without having to type “Y” in the r console? I would like to schedule my update but having to manually type Y is preventing me from doing so. Putting an unquoted Y in the code like I found on stack overflow doesn’t work because it still stops and asks for user input.


r/R_Programming • • Jan 22 '18

How do I retrieve a list of the value names of a table?

1 Upvotes

How do I retrieve a list of the value names of a table such as the following? Desired output: (Apple, Banana, Grape, Orange, blah1, blah2, blah3, blah4, cat, dog, oth, 51.50........)?? The order must remain as it is in the table.

$fruit
Apple Banana Grape Orange  
       1      7    5     1 

$blahs
blah1 blah2  blah3      blah4
       1     8     1     1 

$animals
cat  dog oth 
  2   1   1 

$amounts
 51.50 549.99 585.00 599.99 
     1      1        1           1 

$dates
12/1/2017
 4

$flavors
chocolate vanilla 
3              1 

r/R_Programming • • Jan 04 '18

Question : dcast() value.var do not find input

1 Upvotes

Hi,

I am trying to use dcast(), but I can't figure out why I get this error: "Error : value.var (mpg) not found in input". Dcast can't seem to find "mpg" which I made a measure.vars in the melt function.

Can you guys help me?

Here is my code:

data("mtcars")

install.packages("reshape")

library(reshape)

install.packages("reshape2")

library(reshape2)

mdata <- melt(mtcars, id=c("gear","cyl"), measure.vars = c("mpg","hp"))

castData <- dcast(mdata, gear ~ cyl, value.var="mpg")

castData

r/R_Programming • • Jan 03 '18

I would like to learn R, but I have a doubt

1 Upvotes

Hello friends, a question, do people who program in R, must know other languages or knowing only R can access a job? Thanks.


r/R_Programming • • Jan 02 '18

Beginner tutorial videos

1 Upvotes

Can anyone recommend a comprehensive tutorial series for R in video format? Are there any websites where one can find practice problems?