Levels for several variables with for loop (R) - r

I want to see the level as an output of several variables.
Let's say I have 9 variables, and I could do it like this:
levels(myd$tel_1)
levels(myd$tel_2)
levels(myd$tel_3)
...
But I want to improve it by using a for loop. I think it's useless to use the same function instead of the same code copied underneath.
I've tried something like this:
block <-
c("tel_1",
"tel_2",
"tel_3",
"tel_4",
"tel_5",
"tel_6",
"tel_7",
"tel_8",
"tel_9")
for (j in block) {
levels(myd[[j]])
}
But this does not work, no error message, nothing. Only the code copied into the console.
Thanks for help!

Related

Loop in R not functioning correctly

I have problems with my coded loop for some of my variables. I have run it firstly with one variable and it ran fine. Upon entering a different variable and different data range, the loop did not include all the outputs that it should have done according to the loop. There was no error message. The code for the loop is below:
geartype=data[179:187]
data_stats_structure_gt=as.data.frame(matrix(nrow=0,ncol=3))
names(data_stats_structure_gt)=c('Gear.Type','Man_Kendall_pvalue','Man_Kendall_tauv')
for (i in 1:ncol(geartype)){
dat=geartype[,i]
dat=as.data.frame(cbind(data$ï..Year,dat))
ds=names(geartype)[i]
names(dat)=c("Year",names(geartype)[i])
dat=dat[-which(is.na(dat[,2])==TRUE),]
if(nrow(dat)>=3){
output=as.data.frame(matrix(nrow=1,ncol=3))
names(output)=c('Gear.Type','Man_Kendall_pvalue','Man_Kendall_tauv')
output$Gear.Type=names(geartype)[i]
output$Man_Kendall_pvalue=mk.test(dat[,2])$p.value
output$Man_Kendall_tauv=mk.test(dat[,2])$estimates[3]
data_stats_structure_gt=rbind(data_stats_structure_gt,output)
}
}

Applying a System Call for ImageJ over a List in R

I am working with a large number of image files within several subdirectories of one parent folder.
I am attempting to run an ImageJ macro to batch-process the images (specifically, I am trying to stitch together a series of images taken on the microscope into single images). Unfortunately, I don't think I can't run this as an ImageJ Macro because the images were taken with varying grid sizes, ie some are 2x3, some are 3x3, some are 3x2, etc.
I've written an R script that is able to evaluate the image folders and determine the grid size, now I am trying to feed that information to my ImageJ macro to batch process the folder.
The issue I am running into seems like it should be easy to solve, but I haven't had any luck figuring it out: in R, I have a data.frame that I need to pass to the system command line-by-line with the columns concatenated into a single character string delimited by *'s.
Here's an example from the data.frame I have in R:
X xcoord ycoord input
1 4_10249_XY01_Fused_CH2 2 3 /XY01
2 4_10249_XY02_Fused_CH2 2 2 /XY02
3 4_10249_XY03_Fused_CH2 3 3 /XY03
4 4_10249_XY04_Fused_CH2 2 2 /XY04
5 4_10249_XY05_Fused_CH2 2 2 /XY05
6 4_10249_XY06_Fused_CH2 2 3 /XY06
Here's what each row needs to be transformed into so that ImageJ can understand it:
4_10249_XY01_Fused_CH2*2*3*/XY01
4_10249_XY02_Fused_CH2*2*2*/XY02
4_10249_XY03_Fused_CH2*3*3*/XY03
4_10249_XY04_Fused_CH2*2*2*/XY04
4_10249_XY05_Fused_CH2*2*2*/XY05
4_10249_XY06_Fused_CH2*2*3*/XY06
I tried achieving this with a for loop inside of a function that I thought would pass each row into the system command, but the macro only runs for the first line, none of the others.
macro <- function(i) {
for (row in 1:nrow(i)) {
df<-paste(i$X, i$xcoord, i$ycoord, i$input, sep='*')
}
system2('/Applications/Fiji.app/Contents/MacOS/ImageJ-macosx', args=c('-batch "/Users/All Stitched CH2.ijm"', df))
}
macro(table)
I think this is because the for loop is not maintaining the list-form of the data.frame. How do I concatenate the table by row and maintain the list-structure? I don't know if I'm asking the right question, but hopefully I'm close enough that someone here understands what I'm trying to do.
I appreciate any help or tips you can provide!
Turns out taking a break helps a lot!
I came back to this after lunch and came up with an easy solution (duh!)- I thought I would post it in case anyone comes along later with a similar issue.
I used stringr to combine my datatable by columns, then put them back into list form using as.list. Finally, for feeding the list into my macro, I edited the macro to only contain the system command and then used lapply to apply the macro to my list of inputs. Here is what my code looks like in the end:
library(stringr)
tablecombined<- str_c(table$X, table$xcoord, table$ycoord, table$input, sep = "*")
listylist<-as.list(tablecombined)
macro <- function(i) {
system2('/Applications/Fiji.app/Contents/MacOS/ImageJ-macosx', args=c('-batch "/Users/All Stitched CH2.ijm"', i))
}
runme<- lapply(listylist, macro)
Note: I am using the system2 command because it can take arguments, which is necessary for me to be able to feed it a series of images to iterate over. I started with the solution posted here: How can I call/execute an imageJ macro with R?
but needed additional flexibility for my specific situation. Hopefully someone may find this useful in the future when running ImageJ Macros from R!

selecting small vif variables in r

I am trying to write an r function to select covariates with small VIF.
Here is my code:
ea=read.csv("ea.csv")
library(car)
fullm<-lm(appEuse~.,data=ea)
cov<-names(ea)
ncov<-length(cov)
vifs<-rep(NA,ncov)
include<-rep(NA,ncov)
for (i in 1:ncov){
vifs[i]<-vif(fullm)[i]
if (vifs[i]<10){
include[i]<-cov[i+1]
}
}
Error in if (vifs[i] < 10) { : missing value where TRUE/FALSE needed
I was trying to set for loop from 1 to ncov-1, then got argument is of length zero.
Is there a way to go around it?
Could be wrong, but looks like you're trying to loop an if statement over a list of NAs:
vifs<-rep(NA,ncov)
is later referenced at
if (vifs[i]<10){
Could this be your issue?

print entire string to console without truncating and without adjusting a global setting

I have read these SO posts on getting rstudio to print out without truncating:
list output truncated - How to expand listed variables with str() in R
avoid string printed to console getting truncated (in RStudio)
The answers there involve making a adjustment to studio settings which would then cover all future outputs to the console.
Is there a ad hoc way to get r to print an entire string to the console?
I tried:
library(tidyverse)
library(foreach)
mystring <- foreach(i = 1:52) %do% {
paste0("'_gaWeek",i,"'!A16:B;")
} %>% unlist %>% toString()
print(mystring, len = length(mystring))
> print(mystring, len = length(mystring))
[1] "'_gaWeek1'!A16:B;, '_gaWeek2'!A16:B;, '_gaWeek3'!A16:B;, '_gaWeek4'!A16:B;, '_gaWeek5'!A16:B;, '_gaWeek6'!A16:B;, '_gaWeek7'!A16:B;, '_gaWeek8'!A16:B;, '_gaWeek9'!A16:B;, '_gaWeek10'!A16:B;, '_gaWeek11'!A16:B;, '_gaWeek12'!A16:B;, '_gaWeek13'!A16:B;, '_gaWeek14'!A16:B;, '_gaWeek15'!A16:B;, '_gaWeek16'!A16:B;, '_gaWeek17'!A16:B;, '_gaWeek18'!A16:B;, '_gaWeek19'!A16:B;, '_gaWeek20'!A16:B;, '_gaWeek21'!A16:B;, '_gaWeek22'!A16:B;, '_gaWeek23'!A16:B;, '_gaWeek24'!A16:B;, '_gaWeek25'!A16:B;, '_gaWeek26'!A16:B;, '_gaWeek27'!A16:B;, '_gaWeek28'!A16:B;, '_gaWeek29'!A16:B;, '_gaWeek30'!A16:B;, '_gaWeek31'!A16:B;, '_gaWeek32'!A16:B;, '_gaWeek33'!A16:B;, '_gaWeek34'!A16:B;, '_gaWeek35'!A16:B;, '_gaWeek36'!A16:B;, '_gaWeek37'!A16:B;, '_gaWeek38'!A16:B;, '_gaWeek39'!A16:B;, '_gaWeek40'!A16:B;, '_gaWeek41'!A16:B;, '_gaWeek42'!A16:B;, '_gaWeek43'!A16:B;, '_gaWeek44'!A16:B;, '_gaWeek45'!A16:B;, '_gaWeek46'!A16:B;, '_gaWeek47'!A16:B;, '_gaWeek48'!A16:B;, '_gaWeek49'!A16:B;, '_gaWeek50'!A16:B;, '_ga... <truncated>
It's truncated. Is there an ad hoc way around this without changing rstudio settings? Such as by a function argument? I tried print() here.
Also, how do I get rid of the comma separator in between each instance above?
The short answer is "no" since, the option limiting the print is in the IDE itself, which you can't control from your program itself (I'm assuming you're not some crazy hacker here), and not a language feature. It's like trying to stop "WINDOWS" from doing things (although not).
Seems to me the easiest way (ad hoc) is to turn it on, do whatever, then turn it off. If you insist on not doing that, you need to write your own function:
myprint<- function(somestring,idelimit=100) {
for(i in seq(1,nchar(somestring),idelimit+1)) {
print(substr(somestring,i,i+idelimit));
}
}
I'm not a fluent R coder so let me know if you catch a syntax error. The idea is simple - idelimit should be wherever studio truncates (I chose 100 arbitrarily), and basically you're doing the splitting yourself so string is printed line after line without truncation. Each time you take a portion at most idelimit long from somestring and print it.

Problems with while loop in R

I've a data frame with several variables in R. I want to use a while loop to go through all of the records and assign a new value to the record. I tried while loop which look like this:
h$est=""
i=0
while (i<188){
if (h$nr==2) {h$est=="X"}
else {h$est==21}
i=i+1
}
and it is not working at all.
I know how to make it via
h$est[h$nr==2]<-"X"
But I'd like to make it with while loop.
Ok I figure it out, The code should be like that:
for (i in 1:length(h$Treatment)){
if (h$Treatment[i]=="ASP") {h$est[i]=3}
else {h$est[i]=1}
}

Resources