How to check if data frame exists in R environment? [duplicate] - r

This question already has an answer here:
Verify object existence inside a function in R [duplicate]
(1 answer)
Closed 5 years ago.
I am trying to add rows to a data frame if it exists, or assign it to initial data frame in case it doesn't exists. I have tried exists(), missing(), etc, but nothing is working for me.
exists(data) && is.data.frame(get(data))
Error in exists(data) : object 'data' not found
Any help would be highly appreciated.
I am trying to do something like
if(exists(data))
data <- rbind(data,new_data)
else
data <- new_data

If you read the documentation you’ll see that it says that exists requires
a variable name (given as a character string).
In other words, write:
exists('data') && is.data.frame(get('data'))

Related

What's the difference between using 'mean(Complete_measurements$`High Freq (Hz)`)' and 'mean(Complete_measurements[4])'? [duplicate]

This question already has answers here:
The difference between bracket [ ] and double bracket [[ ]] for accessing the elements of a list or dataframe
(11 answers)
Dynamically select data frame columns using $ and a character value
(10 answers)
Closed 4 months ago.
This post was edited and submitted for review 4 months ago and failed to reopen the post:
Original close reason(s) were not resolved
So I'm working through a multivariate analysis for a project and trying to familiarize myself with R as I do so, and I keep getting the error
Warning message:
In mean.default(Complete_measurements[4]) :
argument is not numeric or logical: returning NA
I'm not using 'mean.default' anywhere, but I have tracked the specific location to just my mean(complete_measurements[4])
I've noticed that when I instead use mean(Complete_measurements$High Freq (Hz)) it seems to work fine, but since I'm intending to use an array of data (ie columns 4-11), I was wondering if there was an easy solution to do so?
Thanks in advance!
So I've tried and gotten:
> mean(Complete_measurements$`High Freq (Hz)`)
[1] 6689.791
> mean(Complete_measurements[4])
[1] NA
Warning message:
In mean.default(Complete_measurements[4]) :
argument is not numeric or logical: returning NA
I expected to get the same result for both? So the 6689.791, but I'm not sure why I get the error instead
Edit: while I get an answer when using '''mean(Complete_measurements[[4]])''', it gives me a different one - any reason why?

Cannot import a simple vector in R : why? [duplicate]

This question already has answers here:
R eval parse character limit
(1 answer)
Command Lines error in Rstudio console
(1 answer)
Closed 1 year ago.
I would like to import in R this vector that contains the names of several variables :
names14 <- c("INAMI","HcwProfession","code_qualif","espaces","start_convention","end_convention","date_adh_convention","date_refus_convention","n_limitations","jour_a1","h_s_a1","m_s_a1","h_e_a1","m_e_a1","jour_b1","h_s_b1","m_s_b1","h_e_b1","h_e_b1","na1","type1","INAMI1","lieu1","postal1","ins1","local1","province1","com1","start1","end1","jour_a2","h_s_a2","m_s_a2","h_e_a2","m_e_a2","jour_b2","h_s_b2","m_s_b2","h_e_b1","h_e_b1","na2","type2","INAMI2","lieu2","postal2","ins2","local2","province2","com2","start2","end2","jour_a3","h_s_a3","m_s_a3","h_e_a3","m_e_a3","jour_b3","h_s_b3","m_s_b3","h_e_b1","h_e_b1","na3","type3","INAMI3","lieu3","postal3","ins3","local3","province3","com3","start3","end3","jour_a4","h_s_a4","m_s_a4","h_e_a4","m_e_a4","jour_b4","h_s_b4","m_s_b4","h_e_b1","h_e_b1","na4","type4","INAMI4","lieu4","postal4","ins4","local4","province4","com4","start4","end4","jour_a5","h_s_a5","m_s_a5","h_e_a5","m_e_a5","jour_b5","h_s_b5","m_s_b5","h_e_b1","h_e_b1","na5","type5","INAMI5","lieu5","postal5","ins5","local5","province5","com5","start5","end5","jour_a6","h_s_a6","m_s_a6","h_e_a6","m_e_a6","jour_b6","h_s_b6","m_s_b6","h_e_b1","h_e_b1","na6","type6","INAMI6","lieu6","postal6","ins6","local6","province6","com6","start6","end6","jour_a7","h_s_a7","m_s_a7","h_e_a7","m_e_a7","jour_b7","h_s_b7","m_s_b7","h_e_b1","h_e_b1","na7","type7","INAMI7","lieu7","postal7","ins7","local7","province7","com7","start7","end7","jour_a8","h_s_a8","m_s_a8","h_e_a8","m_e_a8","jour_b8","h_s_b8","m_s_b8","h_e_b1","h_e_b1","na8","type8","INAMI8","lieu8","postal8","ins8","local8","province8","com8","start8","end8","jour_a9","h_s_a9","m_s_a9","h_e_a9","m_e_a9","jour_b9","h_s_b9","m_s_b9","h_e_b1","h_e_b1","na9","type9","INAMI9","lieu9","postal9","ins9","local9","province9","com9","start9","end9","jour_a10","h_s_a10","m_s_a10","h_e_a10","m_e_a10","jour_b10","h_s_b10","m_s_b10","h_e_b1","h_e_b1","na10","type10","INAMI10","lieu10","postal10","ins10","local10","province10","com10","start10","end10","jour_a11","h_s_a11","m_s_a11","h_e_a11","m_e_a11","jour_b11","h_s_b11","m_s_b11","h_e_b1","h_e_b1","na11","type11","INAMI11","lieu11","postal11","ins11","local11","province11","com11","start11","end11","jour_a12","h_s_a12","m_s_a12","h_e_a12","m_e_a12","jour_b12","h_s_b12","m_s_b12","h_e_b1","h_e_b1","na12","type12","INAMI12","lieu12","postal12","ins12","local12","province12","com12","start12","end12","jour_a13","h_s_a13","m_s_a13","h_e_a13","m_e_a13","jour_b13","h_s_b13","m_s_b13","h_e_b1","h_e_b1","na13","type13","INAMI13","lieu13","postal13","ins13","local13","province13","com13","start13","end13","jour_a14","h_s_a14","m_s_a14","h_e_a14","m_e_a14","jour_b14","h_s_b14","m_s_b14","h_e_b1","h_e_b1","na14","type14","INAMI14","lieu14","postal14","ins14","local14","province14","com14","start14","end14","jour_a15","h_s_a15","m_s_a15","h_e_a15","m_e_a15","jour_b15","h_s_b15","m_s_b15","h_e_b1","h_e_b1","na15","type15","INAMI15","lieu15","postal15","ins15","local15","province15","com15","start15","end15","jour_a16","h_s_a16","m_s_a16","h_e_a16","m_e_a16","jour_b16","h_s_b16","m_s_b16","h_e_b1","h_e_b1","na16","type16","INAMI16","lieu16","postal16","ins16","local16","province16","com16","start16","end16","jour_a17","h_s_a17","m_s_a17","h_e_a17","m_e_a17","jour_b17","h_s_b17","m_s_b17","h_e_b1","h_e_b1","na17","type17","INAMI17","lieu17","postal17","ins17","local17","province17","com17","start17","end17","jour_a18","h_s_a18","m_s_a18","h_e_a18","m_e_a18","jour_b18","h_s_b18","m_s_b18","h_e_b1","h_e_b1","na18","type18","INAMI18","lieu18","postal18","ins18","local18","province18","com18","start18","end18","jour_a19","h_s_a19","m_s_a19","h_e_a19","m_e_a19","jour_b19","h_s_b19","m_s_b19","h_e_b1","h_e_b1","na19","type19","INAMI19","lieu19","postal19","ins19","local19","province19","com19","start19","end19","jour_a20","h_s_a20","m_s_a20","h_e_a20","m_e_a20","jour_b20","h_s_b20","m_s_b20","h_e_b1","h_e_b1","na20","type20","INAMI20","lieu20","postal20","ins20","local20","province20","com20","start20","end20","jour_a21","h_s_a21","m_s_a21","h_e_a21","m_e_a21","jour_b21","h_s_b21","m_s_b21","h_e_b1","h_e_b1","na21","type21","INAMI21","lieu21","postal21","ins21","local21","province21","com21","start21","end21","jour_a22","h_s_a22","m_s_a22","h_e_a22","m_e_a22","jour_b22","h_s_b22","m_s_b22","h_e_b1","h_e_b1","na22","type22","INAMI22","lieu22","postal22","ins22","local22","province22","com22","start22","end22","jour_a23","h_s_a23","m_s_a23","h_e_a23","m_e_a23","jour_b23","h_s_b23","m_s_b23","h_e_b1","h_e_b1","na23","type23","INAMI23","lieu23","postal23","ins23","local23","province23","com23","start23","end23","jour_a24","h_s_a24","m_s_a24","h_e_a24","m_e_a24","jour_b24","h_s_b24","m_s_b24","h_e_b1","h_e_b1","na24","type24","INAMI24","lieu24","postal24","ins24","local24","province24","com24","start24","end24")
But R tells me something is missing (it shows "+", telling me to add something), but I don't understand what ? It works if I shorten the vector, maybe it's too long ?

Column value not getting updated based on pattern search of another column in a dataframe in R [duplicate]

This question already has an answer here:
dplyr summarize output - how to save it
(1 answer)
Closed 2 years ago.
I have a dataframe in R which has two columns Machine Name (character) and Region (factor). They have values as follows (few examples):
Machine.Name Region
1233.corp.pdo.om APAC
xyz.om Europe
345.corp.pdo.cm Europe
abc12.cm Americas
So I want to update the region with pdo for the machines which have corp.pdo in them. But when I am trying to update its not happening using if else.
I have tried using the below code,it doesn't throw error but doesn't update the value as desired:
df3_machine_region %>%mutate(Region= if_else(str_detect(Machine.Name, "corp.pdo"), "corp.pdo", as.character(Region)))
The output is still the same as above.
Machine.Name Region
1233.corp.pdo.om pdo
xyz.om Europe
345.corp.pdo.cm pdo
abc12.cm Americas
I have tried to do some troubleshooting but no success so far.
Your attempt actually works, the only thing missing is assigning the data back to original dataframe.
df3_machine_region <- df3_machine_region %>%
mutate(Region= if_else(str_detect(Machine.Name, "corp.pdo"),
"corp.pdo", as.character(Region)))
However, it works here because of co-incidence not because the logic is correct. You are using regex here and "." has a special meaning in regex which needs to be escaped (\\) or use fixed = TRUE. Try this :
df3_machine_region$Region[grep("corp.pdo", df3_machine_region$Machine.Name, fixed = TRUE)] <- 'corp.pdo'

R - how to write a function to read a CSV file [duplicate]

This question already has answers here:
Calculate the mean of one column from several CSV files
(2 answers)
Closed 4 years ago.
I have CSV files named "001", "002",..."100" stored in the working directory. I need to write a function to read any of these files. I tried the function below, but it doesn't work.
func = function(ID)
{
inp = read.csv("ID.csv")
}
I think this is because "ID.csv" is a character whereas ID is a numeric variable, but I am not sure. Can someone please explain the reason and suggest the right code?
Sounds like you sort of understand the problem. "ID.csv" is a string literal and it is literally looking for a file named ID.csv. If I were you, I would input ID as a string like you have it (i.e. "001" instead of 1). Then try this:
func = function(ID)
{
inp = read.csv(paste(ID,".csv",sep=""))
}

Accessing dynamically named tables using a new object in R [duplicate]

This question already has an answer here:
How to call an object with the character variable of the same name
(1 answer)
Closed 7 years ago.
I have some sequentially labeled data frames i.e frame_1 frame_2 e.t.c... I would like to access them in a sequential manner possibly using a loop
one way that makes sense to me is to assign the name of the data frame I want to access to an object, then pass that object to a function i.e
varname<-paste("frame_",1,_sep="")
then call my function
function(varname)
But R appears to call the function on a string varname, and not the object with the same name as varname.
Is there way I can do what I want?
Thanks.
I found out you can parse a string as an R command using a combination of eval and parse, so for instance :
function( eval( parse(text=paste0("name_",1))) )
In a loop:
for( i in 1:length(holder)){
function(eval( parse(text=paste0("frame_",i))) )
}

Resources