loop problems make frequency table in R - r

library(MASS)
with (survey,
for (variable in names(Filter(is.factor, survey))) {
print(table(variable))
}
)
I'd like to make frequency tables for all factor variables.
But, it doesn't work.
need some help.

Here is another solution:
lapply(Filter(is.factor, survey), table)

Here is a slight modification that works.
for (variable in names(Filter(is.factor, survey))) {
print(table(survey[[variable]]))
}
To use with() with a character as you have tried you could do (not necessarily recommended):
with(survey,
for (variable in names(Filter(is.factor, survey))) {
print(table(eval(as.symbol(variable))))
}
)

Related

R - How to remove NAs when using ASSIGN with a FOR LOOP?

I have some code which imports a data frame (keywordsDF), and whilst using a FOR LOOP, it (by using the colnames), creates new variables depending on how many columns there are in it:
keywordsDF = read_excel("//Users//n//Desktop//Keywords.xlsx")
keywordList = colnames(keywordsDF)
for (i in seq_along(keywordList)) {
assign(keywordList[i], keywordsDF[keywordList[i]])
}
This all works fine. However, as the columns are of a different length, it imports NAs into the data.
I would normally remove those NAs for each column like:
consumption = keywordsDF$Consumption[!is.na(keywordsDF$Consumption)]
But I am not sure how to do it in the FOR LOOP (where I don't know the column names). I.e. where do I put the !is.na? As nothing seems to work for me.
You can use keywordsDF[[keywordList[i]]] any place you would use keywordsDF$Consumption, so
# spaced to make the substitution line up
consumption = keywordsDF$Consumption [!is.na( keywordsDF$Consumption )]
assign(keywordList[i], keywordsDF[[keywordList[i]]][!is.na( keywordsDF[[keywordList[i]]] )]
But I'd encourage you not to use assign like this... a list might be nicer like kw_no_na = lapply(keywordsDF[keywordList], na.omit), then use kw_no_na$consumption or kw_no_na[["consumption"]].
You could simply use na.omit() ?
for (i in seq_along(keywordList)) {
assign(keywordList[i], na.omit(keywordsDF[keywordList[i]]))
}

Loop works outside function but in functions it doesn't.

Been going around for hours with this. My 1st question online on R. Trying to creat a function that contains a loop. The function takes a vector that the user submits like in pollutantmean(4:6) and then it loads a bunch of csv files (in the directory mentioned) and binds them. What is strange (to me) is that if I assign the variable id and then run the loop without using a function, it works! When I put it inside a function so that the user can supply the id vector then it does nothing. Can someone help ? thank you!!!
pollutantmean<-function(id=1:332)
{
#read files
allfiles<-data.frame()
id<-str_pad(id,3,pad = "0")
direct<-"/Users/ped/Documents/LearningR/"
for (i in id) {
path<-paste(direct,"/",i,".csv",sep="")
file<-read.csv(path)
allfiles<-rbind(allfiles,file)
}
}
Your function is missing a return value. (#Roland)
pollutantmean<-function(id=1:332) {
#read files
allfiles<-data.frame()
id<-str_pad(id,3,pad = "0")
direct<-"/Users/ped/Documents/LearningR/"
for (i in id) {
path<-paste(direct,"/",i,".csv",sep="")
file<-read.csv(path)
allfiles<-rbind(allfiles,file)
}
return(allfiles)
}
Edit:
Your mistake was that you did not specify in your function what you want to get out from the function. In R, you create objects inside of function (you could imagine it as different environment) and then specify which object you want it to return.
With my comment about accepting my answer, I meant this: (...To mark an answer as accepted, click on the check mark beside the answer to toggle it from greyed out to filled in...).
Consider even an lapply and do.call which would not need return being last line of function:
pollutantmean <- function(id=1:332) {
id <- str_pad(id,3,pad = "0")
direct_files <- paste0("/Users/ped/Documents/LearningR/", id, ".csv")
# READ FILES INTO LIST AND ROW BIND
allfiles <- do.call(rbind, lapply(direct_files, read.csv))
}
ok, I got it. I was expecting the files that are built to be actually created and show up in the environment of R. But for some reason they don't. But R still does all the calculations. Thanks lot for the replies!!!!
pollutantmean<-function(directory,pollutant,id)
{
#read files
allfiles<-data.frame()
id2<-str_pad(id,3,pad = "0")
direct<-paste("/Users/pedroalbuquerque/Documents/Learning R/",directory,sep="")
for (i in id2) {
path<-paste(direct,"/",i,".csv",sep="")
file<-read.csv(path)
allfiles<-rbind(allfiles,file)
}
#averaging polutants
mean(allfiles[,pollutant],na.rm = TRUE)
}
pollutantmean("specdata","nitrate",23:35)

Writing the results of the for loop

We were trying to write the results from a for loop. We tried to use write.table, as.data.frame and other solutions, but with no success. We expect to have a data frame.
Currently we have only the loop, that shows year and values from a matrix which are bigger than 50. Looks like that:
for (i in 1:nrow(dobowe1)) {
if(dobowe1[i,4]>50) {
cat(dobowe1[i,"rok"],dobowe1[i,4], "\n")
}
}
Note: We don't do programming a lot, so it's hard to use other solutions from the questions that already beed asked.
Try to save each element to the vector, like here:
tabela <- numeric(nrow(dobowe1))
for (i in 1:nrow(dobowe1)) {
if(dobowe1[i,4]>50) {
tabela[i] <- paste(dobowe1[i,"rok"],dobowe1[i,4])
}
}
as.data.frame(tabela)
If you just want to visually inspect a subset of your matrix, you can just print out a filtered subset:
# create the filter:
> f <- dobowe1[,4] > 50
# use the filter to subset (index) your data.frame:
> dobowe1[f,c("rok", whatever-4th-var-is-called)]
This will automatically print it out. Or you can write it to a file with ?write.table

removing elements of the environment using a loop

I have 16 elements in the environment called Factor1 to Factor16. I would like to remove them automatically. I wrote that and I cannot understand why that's not working...
for(i in 1:16) {
rm(paste0('Factor',i))
}
sorry for this basic question, I am a beginner!
for(i in 1:16) {
rm(list=paste0('Factor',i))
}
although rm(list=paste0('Factor',1:16)) or rm(list=ls(pattern="Factor"))would be more appropriate...

Add formula into function

I have this example data
install.packages('neuralnet')
library(neuralnet)
DV<-runif(20,min=-3,max=3)
RV_1<-runif(20,min=-3,max=3)
RV_2<-runif(20,min=-3,max=3)
formula<-'RV_1+RV_2'
df<-data.frame(DV=DV,RV_1=DV_1,RV2=RV_2)
and I learn the neural network this way
neuralnet(DV~RV_1+RV_2,data=df,hidden=5)
and everything works well.
But if I need to use it in function for more combinations I need to use it like
testfun<-function(x,y){
nnet<<-neuralnet(x~y,data=df,hidden=5)
}
testfun(DV,formula)
Which doesn't work
I've tried these approaches
testfun<-function(x,y){
nnet<<-neuralnet(print(x,quote=FALSE)~print(y,quote=FALSE),data=df,hidden=5)
}
or
testfun<-function(x,y){
nnet<<-neuralnet(as.symbol(x)~as.symbol(y),data=df,hidden=5)
}
or
testfun<-function(x,y){
nnet<<-neuralnet(get(x)~get(y),data=df,hidden=5)
}
But nothing works. The problem is that I cannot change the formula object and I still cannot go trough.
Any advices how to solve this problem?
Try this?
testfun<-function(x,y) {
neuralnet(as.formula(paste(x, "~", y, sep ="")), data=df, hidden=5)
}
nnet <- testfun("var1", "var2")

Resources