View( ) function in R: How to view part of huge data frame as spreadsheet in R - 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,]

Related

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)

How to merge a set of lists into a single data frame

I am new to R and coding in general, so please bear with me.
I have a spreadsheet that has 7 sheets, 6 of these sheets are formatted in the same way and I am skipping the one that is not formatted the same way.
The code I have is thus:
lst <- lapply(2:7,
function(i) read_excel("CONFIDENTIAL Ratio 062018.xlsx", sheet = i)
)
This code was taken from this post: How to import multiple xlsx sheets in R
So far so good, the formula works and I have a large list with 6 sub lists that appears to represent all of my data.
It is at this point that I get stuck, being so new I do not understand lists yet, and really need the lists to be merged into one single data frame that looks and feels like the source data (so columns and rows).
I cannot work out how to get from a list to a single data frame, I've tried using R Bind and other suggestions from here, but all seem to either fail or only partially work and I end up with a data frame that looks like a list etc.
If each sheets has the same number of columns (ncol) and same names (colnames) then this will work. It needs the dplyr pacakge.
require(dplyr)
my_dataframe <- bind_rows(my_list)

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.

gvisGeoMap is processing, but no map displays

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)

Replacing Rows in a large data frame in R

I have to manually collect some rows so based on the R Cookbook, it recommended me to pre-allocate some memory for a large data frame. Say my code is
dataSize <- 500000;
shoesRead <- read.csv(file="someShoeCsv.csv", head=TRUE, sep=",");
shoes <- data.frame(size=integer(dataSize), price=double(dataSize),
cost=double(dataSize), retail=double(dataSize));
So now, I have some data about shoes which I imported via csv, and then I perform some calculation and want to insert into the data frame shoes. Let's say the someShoeCsv.csv has a column called ukSize and so
usSize <- ukSize * 1.05 #for example
My question is how do I do so? Running the code, noting now I have a usSize variable which was transformed from the ukSize column, read from the csv file:
shoes <- rbind(shoes,
data.frame("size"=usSize, "price"=price,
"cost"=cost, "retail"=retail));
adds to the already large data frame.
I have experimented with doing the list and then rbind but understand that it is tedious and so I am thinking of using this method but still to no avail.
I'm not quite sure what you're trying to do, but if you're trying to replace some of the pre-allocated rows with new data, you could do so like this:
Nreplace = length(usSize)
shoes$size[1:Nreplace] <- usSize
shoes$price[1:Nreplace] <- shoesRead$price
And so on, for the rest of the columns.
Here's some unsolicited advice. Looking at the code you've included, you reference ukSize and price etc without referencing the data frame, which makes it appear like you've done attach(shoesRead). Definitely never use attach(). If you want the price vector, for example, just do shoesRead$price. It's just a little bit more typing for the sake of much more readable code.

Resources