How can I use hash in R in order to the key values has other hash?
In python I would have something like this:
hash = {}
hash["other_hash"] = {}
hash["other_hash"]["value"] = 5
In R I'm trying to use hash library and env structure to create hashs but I can't create one hash inside the key value of other hash.
You can use a list():
hash <- list(other_hash = list(value = 5))
hash$other_hash$value #5
Related
Using hash package in R I created a hast table with keys and values. I want to add new keys and values to the existing hashtable. Is there any way?
Suppose
ht <- hash(keys = letters, values = 1:26)
And I need to add new keys and values to ht.
Is there any way other than
for eg :
ht$zzz <- 45
The documentation for the hash package provides a number of syntax varieties for adding new elements to a hash:
h <- hash()
.set( h, keys=letters, values=1:26 )
.set( h, a="foo", b="bar", c="baz" )
.set( h, c( aa="foo", ab="bar", ac="baz" ) )
The first .set option would seem to be the best for bulk inserts of key value pairs. You would only need a pair of vectors, ordered in such a way that the key value representation is setup the way you want.
Consider this scenario:
Key1 = random key
Key2 = random key
CombinedKey = Key1.encrypt (Key2)
Input = "test"
Step1 = CombinedKey.encrypt (Input)
Step2 = key2.decrypt (step1)
Result = key1.decrypt (step2)
Is Result == "test" if the encryption type is AES? Or for any other encryption algorythm?
No. AES is not a group. For simplicity's sake, let's just say it this way: AES encryption is not commutative. Said another way, since AES is not a group, there is no key X such that encrypting with key Y and then key Z, key X can decrypt in one step. There are no shortcuts.
If you encrypt Input with CombinedKey then only CombinedKey will decrypt it. Using key2 to decrypt Step1 will result in only junk, not an intermediate result.
In R, I'd like to build a key-value paired list from separate key and value columns. In python I would just do something like this:
d = {k:v for k,v in zip(keys, values)}
I want something similar in R that is equivalent to:
list('key1' = 'value1', 'key2' = 'value2', ...)
I've built this with a for-loop but was hoping there is a more elegant R way of doing this.
You can use split to get a list of key/value pair
split(values, keys)
I have two tables: df.author and df.post, which are related by a one-to-many relation. Now I changed the primary key of df.author and I want df.post to mirror the change. In the following R script I use match() in a while loop to compare the foreign key of each row of df.post with the old primary key of df.author and-when they match-replace the foreign key with the new one (form a different column of df.author). Please consider the following:
foreignkey <- c("old_pk1","old_pk2","old_pk3","old_pk4","old_pk5","old_pk1","old_pk7")
df.post <- data.frame(foreignkey,stringsAsFactors=FALSE)
rm(foreignkey)
primarykey_old <- c("old_pk1","old_pk2","old_pk3","old_pk4","old_pk5")
primarykey_new <- c("new_pk1","new_pk2","new_pk3","new_pk4","new_pk5")
df.author <- data.frame(primarykey_old, primarykey_new, stringsAsFactors=FALSE);
rm(primarykey_old); rm(primarykey_new)
i <- 1; N <- length(df.post$foreignkey)
while (i <= N) {
match <- match(df.post$foreignkey[i], df.author$primarykey_old)
if (!is.na(match)) {
df.post$foreignkey[i] <- df.author$primarykey_new[match]
}
i <- i + 1
}
rm(N); rm(i); rm(match)
The script works but because of while doesn't scale efficiently for a large dataset. I have read that using apply() (in my case by converting to a matrix) is usually better than using while. I wonder if it also applies to my case. Because if you look at the loop you see I need to go through every single row of the dataframe to get the foreign key and then through out df.author for a match().
Can I compress the computational time by not using while?
I think this might do everything in a loopless fashion:
df.post$foreignkey[
!length(match(df.post$foreignkey, df.author$primarykey_old))==0] <- # the test
df.author$primarykey_new[match(df.post$foreignkey, df.author$primarykey_old)]
Logic : Only if there is a match then replace the existing value with the matching value.
I am looking over some code that another programmer made where he calls a stored procedure. Before calling it, he creates an Array with the parameters needed for the stored procedure to query the table. He creates the array like this:
param = Array("#Name", 3, 8, "Tom", _
"#Age", 3, 8, 28, _
"#City", 100, 200, "Toronto)
The stored procedure uses #Name, #Age and #City to query the table.
My question is, what are the numbers in between for?
It looks like:
#Name = parameter name
3 = adInteger
8 = length
"Tom" = value
#Age= parameter name
3 = adInteger
8 = length
28 = value
#City= parameter name
100 = length
200 = adVarChar
"Toronto = value
Here is a list for the other ADO Data Types -
http://www.w3schools.com/ado/ado_datatypes.asp
My guess is that he is using an array of params, just something like this: https://stackoverflow.com/a/10142254/2385, where I use an array of params to pass to a function who add the params to the ADO command.
Without comments it's impossible to know for sure or without stepping through the code.
Otherwise, if this is asp.net the best you can do is look at the SqlParameter class and see the properties it has available:
http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlparameter.aspx
I think you have two strong candidates for ParameterName and Value, but the two numerical values could be a few different things. 3 just happens to be the numerical value for SqlDbType.Char and while 100 has no corresponding SqlDbType, the default for that type is NVarChar.
The next number could be precision. Take a look at the Database table and see if you can match those values to the fields. For example, is City VarChar(200)?