Print(paste(names(sound), "goes", sound)) Thus we can get both the name and value by looking up the item by its numeric index: sounds <- c( If we want to loop over the dictionary and get both the keys and the values, we can remember that a dictionary is just a list. Well, there is a function called names which gives you just those so that you can loop over them: sounds <- c( What if, for example, you wanted to loop over the names (keys) instead. This makes sense, as a dictionary is just a list with named values. So, it seems that when looping over a dictionary we will be given the values. Hopefully, you recognise those as the values from the dictionary. If you put this code into a file called dict_loop.R and run it you should see: "meow" To discover how it works, let’s do the naïve thing first and just see what happens when we loop over a dictionary: sounds <- c( Since a dictionary is just a list (vector) with names, we can loop over dictionaries too. When discussing for loops you were told that R allows you to loop over lots of different types of data such as lists and sequences. What happens if you ask for an animal that isn’t in the dictionary? sounds <- c(Įdit dict.R to ask for the sound for the dog and the horse. To get the value alone, you need to use as.character if the value is a string, or as.numeric if the value is a number, e.g. Was printed to the screen as the value with its name (key) is returned. This also means that you don’t need to name every element! This means you can still access the items by index, e.g. Note that an R dictionary is just a list with named elements. Since dictionaries can be quite large and it can sometimes be hard to see which parts are keys and which are values, it is possible to write dictionaries over multiple lines, one line per key-value item: sounds <- c( In this case we’re asking the dictionary to give us the value associated with the key cat and so it will return to us meow. Again, like lists we use the square brackets to ask questions of our data. On the next line we access the data in the dictionary sounds. The key and value are separated by an equals sign, and each key-value pair is separated by a comma. The value is the real data that we want to keep hold of and the name (also called key) is how we can get at the data we want. We have added names (keys) using the syntax name=value, so meow is given the name cat, woof is named dog and neigh is named horse. The three items in the list are meow, woof and neigh. What we did here was create a dictionary by adding names (keys) to the items in the list. Make a new file called dict.R and put this in it: sounds <- c("cat"="meow", "dog"="woof", "horse"="neigh") You thus create them using the same combine ( c) function as used to create a list. One example is a dictionary, which lets you store variables and access them using a key.ĭictionaries in R are created by associating names (keys) with elements in a list. However, there are times when you want to store lots of variables, but access them using more complex relationships. Lists let you store lots of variables, and to access them by their location in the list.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |