Extracting sequence of column names for Shiny - r

I am constructing an RShiny app and am trying to generate the checkboxGroupInput options in an efficient manner.
I'd like to create a vector with every odd column name to avoid doing the below to generate the potential options for the user to select:
checkboxGroupInput("names","Names",c(colnames(df)[ncol(df)],colnames(df)[ncol(df)-2],colnames(df)[ncol(df)-4],etc...))
Is there a way to extract column names with a function like seq or letters? Ideally it would be something like, though of course the below does not work:
vector=c(colnames(Runs)[ncol(Runs)]:colnames(Runs)[3],-2)
Then I'd want to just insert that vector into the checkbox field.
Thanks for your help!

You can proceed with seq setting a negative by :
colnames(df)[seq(from = ncol(df), to = 1, by = -2)]
(It's not related to shiny though.)

Related

Combine lapply and gsub to replace a list of values for another list of values

I am currently looking for a way to simplify searching through a column within a dataframe for a vector of values and replacing each of of those values with another value (also contained within a separate vector). I can run a for loop for this, but it must be possible within the apply family, I'm just not seeing it yet. Very new to using the apply family and could use help.
So far, I've been able to have it replace all instances of the first value in my vector with the new first value in the new vector, it just isn't iterating past the first level. I hope this makes sense. Here is the code I have:
#standardize tank location
old_tank_list <- c("7.C.4","7.C.5","7.C.6","7.C.7","7.C.8","7.C.9","7.C.10","7.C.11")
new_tank_list <- c("7.B.3-4","7.C.3-4","7.C.1-2","7.C.5-6","7.C.7-8","7.C.9-10","7.E.9-10","7.C.11-12")
sapply(df_growth$Tank,function(y) gsub(old_tank_list,std_tank_list,y))
Tank is the name of the column I am trying to replace all of these values within. I haven't assigned it back yet, because I want to test the functionality first. Thanks for any help you can offer.
Hopefully, this image will help. The photo on the left is the column before my function is applied. The column on the right is after. Basically, I just want to batch change text values.
Before and After
library(dplyr)
df %>%
mutate(Tank = recode(Tank, !!!setNames(new_tank_list, old_tank_list)))

How to unnest data and obtain the first element from an array in SparkR?

I am new to SparkR and trying first steps of data preparation.The dataset is something of this kind. I was trying to subset and select significant columns. My question is how can I select a column from an array element. I was trying something like this, which allowed me to select columns by un-nesting data but couldn't unnest and flatten the array to get it's first element. Helpful Link
select.col <- SparkR::select(data,c("parsed.nid","parsed.status","parsed.sections.element[0].name"))
I myself found a way to resolve this issue.This can be done in two simple steps :-
First we need to use explode() in SparkR, to get all the contents in
the list from that column.
Next, we need to use windowPartitionBy() in SparkR to create a
partitions and then we can get anything we want based on our
requirements like row_number(),dense_rank(),rank() etc. Like here we want the first element of the list, so I have used row_number function.
Snippet :
data.select <- SparkR::select(data,c("parsed.nid","parsed.status","parsed.sections"))
names(data.select) <- c("nid","status","sections")
categories <- SparkR::select(data.select,data.select$nid,data.select$status,explode(data.select$sections))
ws <- SparkR::orderBy(SparkR::windowPartitionBy("nid","status","sections"),"nid")
data.final <- SparkR::mutate(categories,row_num = over(row_number(), ws))
##If we want to get the first element of the array.
data.final <- data.final[data.final$row_num==1,]
Please add your suggestions as well.

R varied length vector or list in variable

I am using R to prepare some data for a D3 visualization. The visualization was created using the following structure (this is a single row from a .csv file that is subsequently converted to JSON in javascript).
Joe.Schmoe, joe.schmoe#email.com, Sao Paulo, ["Community01", "Community02", "Community03"],
["workgroup01","workgroup02"]
This is a single row. The headers would be:
Person, Email, Location, Communities, Workgroups
You'll notice that the Communities and Workgroup columns contain lists. Furthermore, these lists will vary in length depending on what Communities and Workgroups each individual is associated with. I recognize that this is probably not best practice with regard to data "tidyness," but it is what this viz is expecting.
So ... in R (which I'm learning), I'm finding it impossible to recreate this structure because, when I try to populate the "communities" or "workgroups" variables, R seems to expect that each variable will be of equal length.
The code that I have is reading from a data.frame which is list of the members of a particular community, and adding the name of that community to a column in a master data.frame of all employees. I'm indexing by email address because it is unique. So this particular loop looks at each individual email address in a data.frame called "commTD" and finds it in a master data.frame called "testr." If it finds it, it looks at the communities variable and either replaces an NA value with the name of the community (in this case "Technical Design"), or if the vector already exists, appends Technical Design to it:
for(i in commTD$email){
if(i %in% testr$email){
tmpList <- testr[which(testr$email ==i) , 'communities']
if(is.na(tmpList)){
tmpList <- list(c("Technical Design"))
}
else{
tmpList <- append(tmpList[[1]][1], 'Technical Design')
}
testr[which(testr$email ==i) , 'communities'] <- list(tmpList)
}
}
This works fine for the initial replacement, but if I append a new community to the list, and then try to pass it back into the testr data.frame, I get an error:
Error in `[<-.data.frame`(`*tmp*`, which(testr$email == i), "communities",
: replacement has 2 rows, data has 1
You'll note that I'm trying to create a list of vectors, which is just one way I've tried to figure this out. I thought maybe I could force R to see the list as a single object, even though it contains multiple items -- or in this case a vector of multiple items.
Is this just impossible in R, to have varied length vectors or lists as a single variable in a data frame?
Data frames are by definition a list of vectors of equal length, so when you ask if this is possible as a class data.frame(), no its not.
You could either use as suggested another type of object like data.table, or another way would be to think of your desired output as a list of unequal vectors, to pass to your js.
That object would look like something like:
dataList <- list(name = c("Joe.Schmoe", "Joe.Bloe"),
email = c("joe.schmoe#email.com", "joe.bloe#email.com"),
location = c("Sao Paulo", "London"),
Communities = list(c("Community01", "Community02", "Community03"),
c("Community02", "Community05", "Community03")
),
Workgroups = list(c("workgroup01","workgroup02"),
c("workgroup01","workgroup03"))
)
Then access each field like a dataframe, for output to your js:
dataList$name
dataList$Communities
etc...
As per Frank's suggestion, if you want to access each entry via the email address, so you can access each entry like this:
data_list[["joe.schmoe#email.com"]]
...then build the list with the names of the email as the index, like so:
data_list = list(`joe.schmoe#email.com`=list(name="Joe",
location="Sao Paulo",
Communities=....),
`joe.bloe#email.com`=list(n‌​ame="Joe", ...))
Then, you can avoid the non-R style of using for() loops, and start the fun of the lapply() family of functions to work on all the entries in a vectorised manner. (See ?lapply for details)
Hope it helps.

Changing hundreds of column names simultaneously in R

I have a data frame with hundreds of columns whose names I want to change. I'm very new to R, so it's rather easy to think through the logic of this, but I simply can't find a relevant example online.
The closest I could sort of get was this:
projectFileAllCombinedNames <- for (i in 1:200){names(projectFileAllCombined)[i+1] <-variableNames[i]}
Basically, starting at the second column of projectFileAllCombined, I want to loop through the columns in the dataframe and assign them the data values in the second data frame. I was able to change one column name manually with this code:
colnames(projectFileAllCombined)[2]<-"newColumnName"
but I can't possibly do that for hundreds of columns. I've spent multiple hours on this and can't crack it with any number of Google searches on "change multiple columns in r" or "change column names in r". The best I can find online is examples where people change a few columns with a c() function and I get how that works, but that still seems to require typing out all the column names as parameters to the function, unless there is a way to just pass the "variableNames" file into that c() function, but I don't know of one.
Will
colnames(projectFileAllCombined)[-1] <- variableNames
not suffice?
This assumes the ordering of columns in projectFileAllCombined is the same as the ordering of the new variable names in variableNames, and that
length(variableNames) == (ncol(projectFileAllCombined) - 1)
The key point here is that the replacement function 'colnames<-'() is vectorised and can replace any number of column names in a single call if passed a vector of replacement values.

How can I modify the variable named in a dataframe cell?

I'm trying to modify a "variable" variable; that is to say, I wish to modify only that variable whose name matches the text in a cell of a dataframe/matrix.
For example, if matrix1[1,1] == "Rupert", I want to perform an operation on the variable Rupert (say, Rupert <- Rupert + 1). But if matrix1[1,1] == "Paddington", I want to perform the operation on the Paddington variable instead.
I've discovered the assign() function which allows me create new variables whose name is that of the text in a matrix, but I haven't been able to figure out how to modify variables in a similar fashion.
Thanks for your attention,
Alistair
Using your example:
var <- matrix1[1,1]
assign(var,get(var)+1)
The get function can be found in the "See also" section of help(assign).

Resources