Creating a loop to read from rows in R - r

I need to read data from each row of a column and replace it in a link for my web-scrapper project, however, I am not able to iterate over the rows, here is my code:
topic <- read.csv("C:/Users/Downloads/amazon-healing-crystals-keywords.csv", header = TRUE)
for (page_topic in names(topic)) {
print(topic[page_topic])
link <- paste0("https://www.amazon.com/s?k=",
page_topic,
"&crid=3KTKLIKI4L0DQ&sprefix=",
page_topic,
"%2Caps%2C160&ref=nb_sb_noss")
}
Your help would be greatly appreciated. Thank you!

Related

How do I retrieve data for a specific date in r?

Let's say if I want to retrieve the closing price for a cryptocurrency on 2022-09-01. I am sure my code is not correct.
BTC.todays_price <- BTC.charts$close %>% filter(BTC.charts$date = '2022-09-01')
Thanks in advance!
Try the following to conditionally select:
BTC.charts[BTC.charts$date == '2022-09-01', ]$close

Writing content inside a file in R language

I am doing a "Noughts and Crosses" in R game for class and I have a problem.
I have to write the names of the two players in a file, which I have already created, but it gives me error because I must have mistaken something or have a order mistake regarding the functions "write.table", the data.frame I created for the names…
I upload the code:
write.table(names,"C://progra//players.txt", append=TRUE)
checknames(names)
names=data.frame(names=c(player1, player2), stringsAsFactors = FALSE)
print(names)
names=checknames(names)
}
checknames<-function(names)
{
print(names)
file.exists(players)
B=read.table("C://progra//players.txt")
player1=readline(prompt="Player 1, what's your name?: ")
for(i in 1:nrow(B))
{
if(player1==B[i,1])
Any suggestion Will be welcomed!
Thank you in advance!

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)

Warning meassage: number of items to replace is not a multiple of replacement length

I got warnings when running this code.
For example, when I put
tm1<- summary(tmfit)[c(4,8,9)],
I can get the result, but I need to run this code for each $i$.
Why do I get this error?
Is there any way to do this instead of via a for loop?
Specifically, I have many regressants ($y$) with the same two regressors ($x$'s).
How I can get these results of regression analysis(to make some comparisons)?
dreg=read.csv("dayreg.csv")
fundr=read.csv("fundreturnday.csv")
num=ncol(fundr)
exr=dreg[,2]
tm=dreg[,4]
for(i in 2:num)
{
tmfit=lm(fundr[,i]~exr+tm)
tm1[i]<- summary(tmfit)[c(4,8,9)]
}
Any help is highly appreciated
Try storing your result into a list instead of a vector.
dreg=read.csv("dayreg.csv")
fundr=read.csv("fundreturnday.csv")
num=ncol(fundr)
exr=dreg[,2]
tm = list()
for(i in 2:num)
{
tmfit=lm(fundr[,i]~exr+tm)
tm1[[i]]<- summary(tmfit)[c(4,8,9)]
}
You can look at an element in the list like so
tm1[[2]]

Undefined columns selected

I've looked at some answers that were already posted here and was not quite able to find one that would help with my particular situation. I'm new to R so bear with me.
rm(list = ls())
dat=read.csv('C:\\Users\\Casandra\\Downloads\\roaches.csv',as.is=T)
ndata=nrow(dat)
param=read.csv('C:\\Users\\Casandra\\Downloads\\roaches+posterior+after+burnin.csv',as.is=T)
param.med=apply(param,2,mean)
yhat=param.med[1]+param.med[2]*dat$x
png('pred distribution.png')
plot(dat$x,dat$y,xlim=c(0,1),ylim=c(0,1.5),xlab='Covariate x',col='grey',ylab='')
lines(dat$x,yhat,col='red')
nsim=nrow(param)
yhat1=yhat2=matrix(NA,ndata,3)
for (i in 1:ndata){
media=param[,1]+param[,2]*dat$x[i]
yhat1[i,]=quantile(media,c(0.025,0.5,0.975))
tmp=rnorm(nsim,mean=media,sd=sqrt(param[,3]))
yhat2[i,]=quantile(tmp,c(0.025,0.5,0.975))
}
lines(dat$x,yhat1[,1],col='orange',lty=2)
lines(dat$x,yhat1[,3],col='orange',lty=2)
lines(dat$x,yhat2[,1],col='grey',lty=2)
lines(dat$x,yhat2[,3],col='grey',lty=2)
dev.off()
Above is my code and this is the error I am receiving
Error in[.data.frame(param, , 3) : undefined columns selected
The dat data set has 2 columns and the param data set has two as well.
Can anyone point me in the right direction for fixing my code?
Data sets are below:
dat: https://drive.google.com/file/d/0B9MIgQ2O0SHnakRzZ2p2bDhIZ2c/edit?usp=sharing
param: https://drive.google.com/file/d/0B9MIgQ2O0SHnTTVMU0E2TTRDR2M/edit?usp=sharing

Resources