
Having pretty much zero familiarity with the sodium package, I think this is mainly because you generate a unique nonce for every row of your data, effectively forcing sodium's vectorised data_encrypt function to operate like a non-vecorised function. NB: Neither of these approaches are particularly performant as the data grows (I haven't benchmarked either against your loop, but I'd be surprised if they were markedly faster).I also provide a tidyverse solution which would be my preference (and in case others may find it useful).That would definitely not be my preference, but I've tried to hack together a base r solution that which, while perhaps not being particularly elegant, should at least contain all of the pieces that you'd need to tailor your own solution.
#HOW TO CREATE A FOR LOOP IN R CODE#
Though you haven't explicitly stated this, I deduce from the code provided that you'd prefer a base-r solution.I'm including code that tries to simulate your particular use-case by using the randomNames package to help generate a data frame with 30000 IDs and random names.It would be good if you could share a usable data for questions like this (e.g.I'm trying to optimize this function using an apply function instead of a for loop, but am struggling to do so. However, as my dataset is huge with many rows, I realise that the for loop is taking too long to perform this request. # Bind the new vectors back to the dataframeĪnd when I want to encrypt the data, I just call this function as such: df <- encryptdata(df) Store the encrypted name, class and nonce SerializedName <- serialize(df$Name, NULL)Ĭipher <- data_encrypt(serializedName, key, rnonce) Generate a singular nonce for each row of data. # Loop through each row to encrypt the Name Column # Generate a list of 0 vectors based on nrows of df I currently have a function that does something like this: key <- "somespecialkey"

I have a dataframe that looks like this which has over 30 thousand rows: ID Name AttrĪnd I am trying to encrypt certain columns of the dataframe using the sodium package.
