Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I have a config.R file which has variable P.
P_val<-function(s){
if(s==1){
return(p<-0.01)
}
else if(s==2){
return(p<-0.031)
}
else if(s==3){
return(p<-0.001)
}
else if(s==4){
return(p<-0.021)
}
else if(s==5){
return(p<-0.1)
}
else if(s==6){
return(p<-0.77)
}
else if(s==7){
return(p<-0.35)
}
else if(s==8){
return(p<-0.66)
}
}
In my main.R file I want to use this P value but the thing is this p variable here is in a loop and I want different value for each loop run. I am showing you a sample demonstration of what I want:
d<-function(num){
for(s in seq(1,8,1)){
x=2*s ##some variable
source("config.R")
P_val(s)
reset(x,p)
}
reset<-function(x,p){
l_val= (x/p) * num
return(l_val)
}
}
I am using source("config.R") in my main.R file but I don't know how to use it as I am getting this error
Error in reset(x, p) : object 'p' not found
You have to assign the value p, like so:
p <- P_val(s)
More explanations on the files and relations between i and p might be helpful here.
Assuming there is a relation between the iteration number i and value of p you can try defining this relation as a function within the config.R file.
Then you just need to source("config.R") wihtin your main.R, as you're already doing, and make a call to the function from within the iterator whenever needed.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
Please excuse my terrible knowledge of the language - just started looking at it a few hours ago.
I'm trying to understand this code and what it produces but quite unsure. Given that the value of inclusions is 10, why is the output what it is?
seps <- tapply(diff, nonCore, function(x) sort(x)[inclusions])
Outputs
"","x"
"ab",23
"ad",15
The value of diff is
"","x"
"1",31
"2",43
"3",37
"4",22
"5",27
"6",13
"7",24
"8",7
"9",26
"10",29
"11",2
"12",15
"13",10
"14",38
"15",23
"16",21
"17",46
"18",10
"19",20
"20",46
"21",20
"22",32
"23",26
"24",11
"25",16
"26",2
"27",13
"28",4
"29",15
"30",18
"31",13
"32",26
"33",1
"34",27
"35",12
"36",10
"37",35
"38",21
"39",9
"40",35
The value of nonCore is
"","x"
"1","ab"
"2","ab"
"3","ab"
"4","ab"
"5","ab"
"6","ab"
"7","ab"
"8","ab"
"9","ab"
"10","ab"
"11","ab"
"12","ab"
"13","ab"
"14","ab"
"15","ab"
"16","ab"
"17","ab"
"18","ab"
"19","ab"
"20","ab"
"21","ad"
"22","ad"
"23","ad"
"24","ad"
"25","ad"
"26","ad"
"27","ad"
"28","ad"
"29","ad"
"30","ad"
"31","ad"
"32","ad"
"33","ad"
"34","ad"
"35","ad"
"36","ad"
"37","ad"
"38","ad"
"39","ad"
"40","ad"
You should supply the code to construct the vectors diff and nonCore, as it is those who could help you need to do massive edits...
That said, what is happening is that your sorting the combination of the vectors according to ab and and ad. ab matches against the first 20 in diff and ad the last 20. Then you just subset the list that is created with the element number that is given by inclusion.
It's the same as running the function without [inclusion] and doing this afterwards:
sep[[1]][10]
sep[[2]][10]
This question already has an answer here:
Getting user provided data out of a database and into R [closed]
(1 answer)
Closed 9 years ago.
I asked this question yesterday and it was answered but I don't think they really understood my question. I need to extract data from a database. But the user provides the list of variables. I need my code to loop through each requested variable, pull the data associated with it and drop it in a data.frame, ready for analysis.
f.extractVariables<-
structure(function
(dbPath,dbName,table,variables
){
# LOAD LIBRARIES
require(RODBC)
require(xlsx)
setwd(dbPath)
db <- odbcConnectAccess2007(dbName)
#This was my idea on how to loop through the variable list but it won't run
for (i in 1:length(variables))
{
dataCollection <- sqlQuery(db, 'SELECT table.variables[[i]]
FROM table;')
}
#This piece was the solution I was given which runs but all it does is parrot back
#the variable list it doesn't retrieve anything.
variables=paste(variables,collapse=",")
query<-paste(paste("SELECT",variables),
"FROM table",sep='\n')
cat(query)
odbcClose(db)
}
)
#And the user provided input looks something like this. I can change the way the
#variable list comes in to make it easier.
dbPath = 'z:/sites/'
dbName = 'oysterSites.accdb'
table = 'tblDataSiteOysterSamplingPlan'
variables= 'nwLon,nwLat,neLon,neLat'
f.extractVariables(dbPath,dbName,table,variables)
In which language you think you are coding? This is not .NET...
Remove this
for (i in 1:length(variables))
{
dataCollection <- sqlQuery(db, 'SELECT table.variables[[i]]
FROM table;')
}
in which 'SELECT table.variables[[i]] FROM table;' will always be the SAME for all the cycles of the loop, amd change with (at least my guess of what you were trying to do)
for (i in 1:length(variables))
{
dataCollection <- sqlQuery(db, paste0('SELECT table.variables[[', i, ']]
FROM table;'))
}
You say that the above does not work. No point to keep reviewing your code, fix the above and then come back
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I have to rename a list of files in a folder so that:
From file named:
> myfile_000.txt
> myfile_001.txt
> ......
to file named:
> myfile_1.txt
> myfile_2.txt
> .......
Total files = 156
I used the following script:
> file.rename(list.files(pattern="myfile_*.txt", paste0("myfile_", 1:156)))
But without success.
Error:
Error in file.rename(list.files(pattern = "myfile_*.txt", paste0("myfile_", :
argument "to" is missing, with no default
The error message tells you, that you didn't specify the new names. It should work if you change the position of one ):
file.rename(list.files(pattern="myfile_*.txt"), paste0("myfile_", 1:156))
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
UPDATE project_phases p2
SET
p2.ClartiyID = #ClarityID,
p2.Start_date = #start_date,
p2.End_date = #End_date,
p2.Planned_efforts = #Planned_efforts
FROM
WSR.dbo.project_phases p2
INNER JOIN WSR.dbo.tb_Project p1 ON p1.ProjectID=p2.ProjectID
WHERE
ProjectID = #ProjectID
but still the error diplays like object name not found even though that table found
If that is your whole query, why are you linking to tb_Project? It seems like this is only to filter on ProjectID, but this column is in Project_Phases so can't you foget the INNER JOIN and just use:
UPDATE project_phases
SET
ClartiyID = #ClarityID,
Start_date = #start_date,
End_date = #End_date,
Planned_efforts = #Planned_efforts
WHERE
ProjectID = #ProjectID