gvisGeoMap is processing, but no map displays - r

I have a table of data that is 3 columns. The names of the columns are "country" "Sum.of.Xcelerate.Exp" and "count.of.acctName". There is data filled out for every country. I want to plot this data on a geomap so I do
test<- read.csv("country_test.csv", header=TRUE)
This works ok. Then I do
plot(gvisGeoMap(test
, "country"
, "Sum.of.XcelerateExp"
,"Count.of.acctName"
, options=list(dataMode="markers")))
The webpage opens, but there is no map. It is the same page you would normally see just no map. Why am I not able to load a map? Thank you.

you need to be a little more specific about the data you are using if you need a better answer, but I tried to create some data for you that matches the column names you provided
require(googleVis)
#simulate some data based on your column names
country=c("Canada","USA","Mexico","Australia")
Sum.of.XcelerateExp=c(50,100,75,25)
Count.of.acctName=c(1,2,3,4)
# put variables into a dataframe called test
test=as.data.frame(list(country=country,
Sum.of.XcelerateExp=Sum.of.XcelerateExp,
Count.of.acctName=Count.of.acctName))
# create map
plot(gvisGeoMap(test,
locationvar="country",
numvar="Sum.of.XcelerateExp",
hovervar="Count.of.acctName",
options=list(dataMode="markers")))
the plot works on my computer (I'm using Google Chrome), but looking at the help file for gvisGeoMap maybe the problem you're having is your countries are in the wrong format? Format 2 of the locationvar says that it can be a country name. Maybe provide us with the first few rows of your data as well as the structure of your dataset
Try running your script with the following functions so we can get a look at what you're working with:
head(test)
str(test)

Related

how do I join and center attributes of the same name in a DataFrame in R?

I'm working with some data in R, and I have a dataframe with data from areas and mining projects, so some areas have more than one type of project. In this case, I would like to gather data from areas that have the same name, differentiating only the types of projects that each area has, as in the example below:
Example of how my table looks
How would I like to organize them:
Do you want to restructure your data, or present a table in use for data visualization?
If you want to restructure your data, you can either use dplyr::chop() or dplyr::nest().
In your case, dplyr::chop() is most likely what you want. The links provided show some great examples. If your frame is called df, this is how you might use it:
nested_df <- chop(df, c(mining, project_number))

Maintain column order when uploading an R data frame to Big Query

I am uploading an R data frame to Big Query with this:
bq_table_upload("project.dataset.table_name", df_name)
It works, but the order of the columns is different in BQ than in R.
Is it possible to get the table to inherit the order of the columns from the data frame? I have looked through the documentation and have not found this option, though it is certainly possible I missed it.
As pointed out in the comments by Oluwafemi Sule, it's only a matter of providing the dataframe in the "fields" attribute like below:
bq_table_upload("project.dataset.table_name", values = df_name, fields=df_name)

Updating a File in R by adding a column/vector

Is there any way that I can update an existing .csv file by adding a column/vector that I have scraped from the web. I have a webscraper that pulls COVID-19 data and I am trying to create a file that has positive cases in columns and each column is the list of cases for a day in each county (x-axis is counties, y-axis is date). I have toyed around with many different ideas at this point and seem to have hit a roadblock. I'm fairly new to r so any ideas would be appreciated!
Packages I am Currently Using/Planning to Use:
library(tidyverse)
library(funModeling)
library(Hmisc)
library(rvest)
library(ggplot2)
CODE:
#writing the original file
positive <- data.frame(Counties= counties_list, "06/12/2020"= positive_data)
positive[is.na(positive)]= 0
positive = positive[-c(76),]
write.csv(positive, "C:/Users/Nathan May/Desktop/Research Files (ABI)/Covid/Data For
Shiny/Positive/Positive Data.csv")
#creating the new vector and updating the existing file with it
datap <- read.csv("C:/Users/Nathan May/Desktop/Research Files (ABI)/Covid/Data For
Shiny/Positive/Positive Data.csv")
positive_data = positive_data[-c(76),]
datap$DATE <- positive_data
NOTE: The end goal is to create a ShinyApp that displays bar charts for postives, recoveries, and deaths by day in each county. This is the data wrangling portion.
First things first, if you are going to use the tidyverse, use tibble instead of data.frame. Tibbles are the Tidyverse version of data frames.
Next, be aware of the structure of your data frame. The way you create your data.frame now (and later probably your tibble) you get a variable "Counties" and one additional variable for each day. That means that you will have to add columns as time passes (the opposite of what you described: Moving along the x axis (along columns) will move along dates while moving along the y-axis (moving along rows) will move along counties). It's possible but I think a bit unconventional. You might want to initialize your data frame with one column for each county and an additional variable called "date". Then whenever you get new data you can add a row in your dataframe instead of a column (so you're "adding a new case" instead of "adding a new variable").
To actually add the row you will have to load the data as you do in your code, create the new row (or column, if you insist) and then "glue" it to the rest of the data.
Depending on how your data looks you can create a single row dataframe using tibble_row() with the same countries as variable names as you have in your main data frame and then glue them together with add_row(datap, your_new_row). Alternatively, if you want to add the row only using position and not column names, you can have the new row as a vector and use rbind() instead of add_row.
If you persist with the "one variable per date" approach there's column equivalents (add_column and cbind) for both these functions.
Hope this helps, Cheers

Changing a List to a Dataframe in R

I have used the "htmltab" library to get data on the NFL draft and combine. The data has been selected fine but they are lists at the moment. I intend to merge them and perform analysis the data. at the moment it looks like this:
image List of combine 2016 1
Whenever I try use the unlist method I lose the headers of the columns and they are still remaining as a list.
any suggestions on this?
urlcom16 <- "http://nflcombineresults.com/nflcombinedata.php?
year=2016&pos=&college="
com16 <- htmltab(doc=urlcom16, which=1)
Try as.data.frame(com16). If it doesn't work, you might not have the same vector length in each list entry.

View( ) function in R: How to view part of huge data frame as spreadsheet in R

I have a huge data frame df in R. Now,if I invoke View(df) then Rstudio does not respond since it's too big. So, I am wondering, if there is any way to view, say first 500 lines, of a data frame as spreadsheet.
(I know it's possible to view using head but I want to see it as spreadsheet since it has too many columns and using head to see too many columns is not really user friendly)
If you want to see first 100 lines of the data frame df as spreadsheet, use
View(head(df,100))
You can look at the dataframe as a matrix such as df[rowfrom:rowto, columnfrom:columnto], for example in your case:
df[1:500,]

Resources