Showing posts with label ODK. Show all posts
Showing posts with label ODK. Show all posts

Saturday, April 19, 2014

R joins - revisited (OpenDataKit merge problem)

I've been hacking around #Opendatakit and after pulling data with it's ODKBriefcase tool, you find yourself with a number of .csv files. The first file is the main flat file while the rest are due to the loops that users were adding to the forms. I won't go further into ODK tool but what i know is that the R merge() function allows us to take two data sets and combine them into one, based on a common
variable. To test this, import the following data by running this command:


       

            # Set working directory
# Read data from the web, but download the files first in .csv
download.file('https://www.dropbox.com/s/w12qrdkbp6gpsg2/survey_jan.csv', destfile='survery.csv', method='wget')
download.file('https://www.dropbox.com/s/w12qrdkbp6gpsg2/survey_jan_member.csv', destfile='survery_jan_member.csv', method='wget')

#Load the data in R.  
data <- read.csv('survey_jan.csv', sep=',', header=TRUE)
member <- read.csv('survey_jan_member.csv', sep=',', header=TRUE)


And to check that it has imported correctly, which is always a good idea, run:
       

# Check the loaded data.
head(data)
head(member)
     
 


We are now tackling a JOIN problem and i always find my self falling back to this JOIN explained page on SO. HERE
For me this is the best part, we now have two data sets; data, which contains a list of survey entries called data, and members, which contains a list including those people as well as additional people who are members of the specific households.
The next step is to combine the two. What we are going to do is select the unique KEYS in the "member" data frame who also appear in the main "data" data frame, and copy their details into a new data frame, along with all the information.

We will refer to the two data frames as x and y. The x data frame is data; and the y is member. In x, the
column containing the list of id's  is called “KEY”, and in y, it is called “PARENT_KEY”. The parameters of the merge function first accept the two table names, and then the lookup columns as by.x or by.y. You should also include all.x=TRUE as a final parameter. This tells the function to keep all the records in x, but only those in y that match.

       

main_survey <- merge(data, member, by.x = "KEY", by.y="PARENT_KEY", all.x = TRUE) 
 



To see what this command has done, type main_survey to show the content of the new data
frame. This should look like:


> head(main_survey)
                                        KEY X.x           SubmissionDate                    sstart
1 uuid:68ec3f5d-078d-4ce9-a197-c3377eee720b   1 Apr 19, 2014 12:07:09 PM Apr 19, 2014 12:05:12 PM
2 uuid:68ec3f5d-078d-4ce9-a197-c3377eee720b   1 Apr 19, 2014 12:07:09 PM Apr 19, 2014 12:05:12 PM
3 uuid:68ec3f5d-078d-4ce9-a197-c3377eee720b   1 Apr 19, 2014 12:07:09 PM Apr 19, 2014 12:05:12 PM
4 uuid:68ec3f5d-078d-4ce9-a197-c3377eee720b   1 Apr 19, 2014 12:07:09 PM Apr 19, 2014 12:05:12 PM
5 uuid:a513043a-0450-47fa-8495-4c2611ece384   2 Apr 19, 2014 12:04:22 PM Apr 19, 2014 12:02:05 PM
6 uuid:a513043a-0450-47fa-8495-4c2611ece384   2 Apr 19, 2014 12:04:22 PM Apr 19, 2014 12:02:05 PM
                       end        today respondent.r_name respondent.position
1 Apr 19, 2014 12:07:03 PM Apr 19, 2014      Ngamita mary           Head food
2 Apr 19, 2014 12:07:03 PM Apr 19, 2014      Ngamita mary           Head food
3 Apr 19, 2014 12:07:03 PM Apr 19, 2014      Ngamita mary           Head food
4 Apr 19, 2014 12:07:03 PM Apr 19, 2014      Ngamita mary           Head food
5 Apr 19, 2014 12:04:18 PM Apr 19, 2014       Jona okello             Prefect
6 Apr 19, 2014 12:04:18 PM Apr 19, 2014       Jona okello             Prefect

Finally, this is a very important note and don't forget that if the by column names were named the same in both x and y (e.g. both called "KEY”), we could specify this
more simply with by="column name" rather than by.x and by.y; and finally, a critical issue when making any join is assuring that the “by” columns are in the same format.

I hope this helps someone out there working with normal joins and also ODK data

Sunday, March 30, 2014

Create an ODK Aggregate AppEngine Instance from Ubuntu 12.04

This tutorial is mainly picked from ODK tutorial here, with a few **nix additions to make a smooth installation for Linux users.

First and foremost why would and Rstats guy be doing with ODK, now think twice - when working with mobile data collection, there is no better tool out there than ODK and it works seamlessly with Google AppEngine. Google AppEngine allows easy hosting of your applications, and your applications' data, on an online server. Learn more about AppEngine and read the terms of service.
For this tutorial, you will set up your own server using AppEngine infrastructure and ODK Aggregate. ODK Aggregate allows you to set up a ready-to-deploy server where you can upload your form submissions, export them as .csv files and send them on to other services like Fusion Tables.
  1. First, you'll need to set up a Google AppEngine account. If this is your first time using AppEngine, Google will send you a text message to confirm your identity, so have a text-enabled phone ready. Note: A given mobile number can only be used once for creating an AppEngine instance.
  2. Once you've confirmed your mobile number and logged in, click "Create an Application."
  3. Give your application an "Application Identifier." This will become part of the URL for your AppEngine instance, e.g. "myurl.appspot.com."
  4. (If you would like to restrict the permissions for who can access your data, read the Security section below. You can edit the Security settings in this step.)
  5. For this tutorial, we will use the default "Open to all Google Account Users." Once you've entered an identifier, you will receive confirmation that your application has registered successfully.
  6. Download the latest version of ODK Aggregate from the ODK Aggregate v1.N.N page.
  7. Unzip it and for Ubuntu Linux i turns into something like ODK Aggregate vx.x.x linux-x64-installer.run file.
  8. Now this is where many people get a little lost - how do you run the .run files. First make sure that the file is executable.  Enter at terminal: “chmod +x filename.run” Then also enter: “./filename.run” with executes and brings setup wizard. You will enter the ODK Aggregate Setup Wizard. Click "Next."
  9. You will be prompted to agree to ODK Aggregate's License Agreement. Then, select the location on your computer where you want the installer files to store the software that will run on Google AppEngine.
  10. Run the Setup Wizard. Enter the ODK instance name (e.g. "odkfunite" for odkfunite.appspot.com) and the Google account associated with that instance.
  11. Once the Setup Wizard finishes configuring ODK Aggregate, you'll be prompted to run the installation script. Note that for Linux you can also run the script manually from the terminal but make sure it is executable,  “chmod +x filename.sh” Then also enter: “./filename.sh” this will then run on your terminal.
Watch out:


Errors “Exception in thread "main" java.lang.UnsupportedClassVersionError: com/google/appengine/tools/admin/AppCfg : Unsupported major.minor version 51.0”


Solution:
Make sure you got Java 7 and above for the latest Aggregate version - check this from Linux terminal by command “java -version”. In addition, make sure that if you already installed java 7, then in the priority list its running first. Command “sudo update-alternatives --config java” and select the number to make it default as shown below.



  • To get a list of your installed Java platforms, run the following command from the terminal:
    sudo update-alternatives --config java
    This will give you a list output similar to this:
    There are 2 choices for the alternative java (providing /usr/bin/java).
       Selection    Path                                           Priority   Status
      ------------------------------------------------------------
      0            /usr/lib/jvm/java-6-oracle/jre/bin/java         1070      auto mode
      1            /usr/lib/jvm/java-7-openjdk-i386/jre/bin/java   1051      manual mode
    * 2            /usr/lib/jvm/java-6-openjdk-i386/jre/bin/java   1069      manual mode
    Press enter to keep the current choice[*], or type selection number: 


  1. You'll be prompted to enter the Google Account and password associated with the AppEngine instance again.
  2. Wait 3-10 minutes for the script to run. Once it's finished, you'll get a "Success" message. If you made a mistake entering the information in the Setup Wizard, you'll find out in this stage. You can re-run the Setup Wizard by going back to the folder where you installed ODK Aggregate and double-click the installer to run it again.
  3. Congratulations! When the script is finished running, ODK Aggregate is now installed on your AppEngine instance. When it's finished running, confirm that it's set up correctly by visiting your AppEngine url (e.g. myurl.appspot.com). You can sign in with your Google Account or click "Anonymous Access" to get in.