Monthly Archives: January 2018

Module 3 – Data Frames

The assignment was to take a set of data and perform operations on it as a data frame, per an example document.

The variables with the initial data were:

Name <- c("Jeb", "Donald", "Ted", "Marco", "Carly", "Hillary", "Berine")
ABC <- c(4, 62, 51, 21, 2, 14, 15)
CBS <- c(12, 75, 43, 19, 1, 21, 19)

I tried turning that into a matrix…

candidates.m <- cbind(Name, ABC, CBS)

…and got a matrix full of strings.

     Name      ABC  CBS
[1,] "Jeb"     "4"  "12"
[2,] "Donald"  "62" "75"
[3,] "Ted"     "51" "43"
[4,] "Marco"   "21" "19"
[5,] "Carly"   "2"  "1"
[6,] "Hillary" "14" "21"
[7,] "Berine"  "15" "19"

So I made a data frame from the data.

candidates.df <- data.frame(Name, ABC, CBS)

The resulting data frame:

     Name ABC CBS
1     Jeb   4  12
2  Donald  62  75
3     Ted  51  43
4   Marco  21  19
5   Carly   2   1
6 Hillary  14  21
7  Berine  15  19

I tried to run the mean command on the data frame, but it didn’t work out in RStudio the way it did in the example. The example showed an error for the text column and then means for the numeric columns, but all I got was the error.

> mean(candidates.df)
[1] NA
Warning message:
In mean.default(candidates.df) :
  argument is not numeric or logical: returning NA

I got the same error when specifying the two numeric columns.

mean(candidates.df[,2:3])

But I could get the mean of a specific column.

> mean(candidates.df[,2])
[1] 24.14286

To get the means of the columns I had to use colMeans on the numeric columns specifically – using colMeans on the full dataframe resulted in an error that “‘x’ must be numeric”.

colMeans(candidates.df[2:3])

Result:

     ABC      CBS
24.14286 27.14286

I could also use rowMeans to get means by row.

> rowMeans(candidates.df[2:3])
[1]  8.0 68.5 47.0 20.0  1.5 17.5 17.0

Using as.matrix(candidates.df) gave the same matrix as I listed at the top of this post.

Data frames are interesting as a means of storing mixed data in a matrix-like structure. It has its caveats when the columns have different modes, but those can be worked around with the right code.

Posted in R Programming | Comments Off

Module 3 – Image Formats

The assignment was to create an image, save it in JPG, PNG, and GIF formats, and compare them. The result is on the page here:

http://jered.0fees.us/module3/

For the image, I took a picture with my phone. The phone stored the photo as a JPG. I then converted that image to the two other formats, and resized all three to a width of 1024 for better web viewing than the larger original.

I was surprised to see the GIF compare reasonably well to the other formats in terms of visual quality. Looking closely I can see that the colors in the GIF are bit washed-out compared to the others, but it wasn’t a difference that stuck out at first glance. I didn’t see a difference between JPG and PNG, though the initial image being in JPG format might have influenced that outcome.

The JPG was easily the most web-friendly of the three, coming in at around 61k. Next was the GIF, at 308k. Then the PNG, the lossless format, was 1220k, far and away the largest. The PNG would definitely be the least web-friendly format, as that size would slow a page’s load time quite a bit (especially a mobile phone using wireless data).

I was able to load the images in both desktop and mobile browsers.

Posted in Web Design Technologies | Comments Off

Module 3 – JavaScript Variables

The assignment asked students to create a web page that uses multiple JavaScript variables. The result is here:

http://jered.0fees.us/module3/

Variables weren’t hard to work with – it’s a similar paradigm to other languages, so declaring variables the first time you use them is familiar enough. I had to work at figuring out using innerHTML – specifically, that running it a second time on the same element will overwrite the contents of the element. I also experimented to see if JavaScript would let me concatenate numbers and strings in a similar fashion to Java (which it does).

Posted in Web Design Technologies | Comments Off

Module 2 – HTML and JavaScript Syntax, Structure, and Coding

For module 2, the class was asked to create an index.html page for the 0fees site account we’d created in module 1. My page is here:

http://jered.0fees.us/

Most of the assignment was review – make a web page, include a title, text, and an image, etc. New for me was adding JavaScript to the page. I didn’t try to do anything fancy – the example given used document.write(), so I used that too. Uploading the page and image wasn’t difficult – I have a lot of past experience working with sites via ftp and sftp.

One element I did pause to research was whether or not to embed the javascript code in an HTML comment. One slide in the presentation showed the code without the comment, but another slide and the assignment example did include it. From what I gathered (mostly from looking at StackFlow questions on the subject), leaving the comment out is a good idea. Modern browsers know about the script tag so they won’t display the text inside, and there are occasional cases where the comment can cause issues (like not being able to use “–” to decrement in the code).

I also felt that I should use an original image instead of dealing with potential copyright issues by borrowing one. I am, unfortunately, a terrible artist.

Posted in Web Design Technologies | Comments Off

Module 2 – Introduction to Basic R Functions

In module 2, the class was asked to test and evaluate R code. We were given two commands:

assignment2 <- c(16, 18, 14, 22, 27, 17, 19, 17, 17, 22, 20, 22)
myMean <- function(assignment2) { return(sum(assignment)/length(someData)) }

The first line creates a dataset labeled assignment2, and the second line creates a function named myMean. Judging by the code, myMean is meant to calculate the mean of the dataset sent to it as an argument.

Unfortunately, the code doesn’t work as written, giving an object not found error. The problem lies in the variables used in the body of the function – neither assignment nor someData have been given values. If the goal is to find the mean of the dataset passed to the function as an argument, then the names of the variables in the function body should match that of the function’s argument.

The function works, then, if it’s rewritten as:

myMean <- function(assignment2) { return(sum(assignment2)/length(assignment2)) }

For clarity it might make sense to change the argument of the function to assignment instead of assignment2. That way there wouldn’t be a potential for confusion between the global variable and the internal variable processed by the function. Because of the scope difference, however, the identical names don’t create a problem for the result – passing a different dataset to the function causes it to return the mean of that dataset, not the dataset contained in the global assignment2 variable.

Posted in R Programming | Comments Off