How to call expression result after paste command in R? - r

I want to get a cell value after dynamically passing its address. So I am trying paste command to join the address of the cell like following:
paste0("DT1$", eval(cols[1]),"[1]")
where DT1 is datatable, cols[1] is refering to 1 column and [1] is first row of that column. While running this I am getting the string(address of the cell):
> paste0("DT1$", eval(cols[1]),"[1]")
[1] "DT1$BCC1[1]"
But I want the value of the cell like if I run:
> DT1$BCC1[1]
[1] 0
So how to run call the result of the paste expression to get value of cell like "0" in previous example. I tried eval() and do.call(), but nothing seems to be working. I am sorry for this basic question as I am new to R. Any help is really appreciated.

You can use eval(), but you have to parse the string "DT1$BCC1[1]" first:
str <-paste0("DT1$", eval(cols[1]),"[1]")
eval(parse(text = str))

The $ dollar is suitable for console use(partial name matching). You should Use the subsetting [ operator.
For example you can call it like this :
DT1[1,cols[1]]
Ore more general :
x= 1
y = "BCC1"
DT1[x,y]
Note that DT1 that here is a data.frame not a data.table. You can do the same thing with a data.table:
DT1[x,y,with=F]

Related

Unique function: looping through variables in R

I am currently in a loop in my R code and I want to use the following code:
distinct.values <- unique(SQL_Table$column.names2[for.num])
column.names2 looks like this
column.names2
[1] "plan" "gender" "marital_status" "acceleration" "extension"
[6] "inflation"
depending on the for.num I want a different variable after the $.
Example:
when for.num = 1.
I want distinct.values to be set as unique(SQL_Table$plan)
when for.num = 2.
I want distinct.values to be set as unique(SQL_Table$gender)
and so on...
How can I do this?
To expand my comment on the original question, there a few different ways to access columns of data frames.
my_df$column_name
When using the $ operator, "column_name" is specified as a literal token in the R script. Note that because the column name is unquoted, this method does not allow variable substitution.
my_df[["column_name"]]
When using the [[ ]] operator, a string (or vector of strings) is expected. In this case, variable substitution is allowed, so the following is valid:
my_col <- "column_name"
my_df[[my_col]]
This would be equivalent to my_df$column_name.
So your code could be modified to read:
unique(SQL_Table[[column.names2[for.num]]])

How to use one on one grep between 2 columns of a data frame in R

I have a dataframe X with 2 columns named "pattern" and "text" in R.
I want to search each pattern within the corresponding text.
For this I am using the grepl command. However, grepl is working fine only in the following scenario where it finds one pattern and tests against each row of the dataframe column:
grepl("findthis",X$text)
However, when I do the following, it only checks the first record of the first column one by one against all records of the second column.
grepl(X$pattern,X$text)
I am looking for a function which would take the first record of X$pattern and check it in the first record of X$text, then take the 2nd record of X$pattern and check it in the second record of X$test.
Is this possible through some library function?
Edit: The solution given by #akrun works as per my requirement.However, I am using a series of grepl commands in a nested ifelse. Simply put it is something like the code below (but with more nesting):
X$result = ifelse(grepl(X$pattern,X$text),1,ifelse(grepl("abc",X$email),2,3)
How do I solve for this?
One option is Map/mapply
unname(mapply(grepl, X$pattern, X$text))
#[1] TRUE FALSE
data
X <- data.frame(text = c("find this text", "Something else"),
pattern = c("find this", "find that"), stringsAsFactors=FALSE)

R Grep Function - working with the outcome matches to fill a column

I am trying to come with a solution for using grep function to a column of data and filling the grep matches with a number 1 and fill the mismatches with O´s but I just can´t come up with a solution (I am totally new to R, so what I am doing is probably going to make some folks lol).
info <- cbind(info, A_DO = ifelse(grep("DO",info[,"ACTUAL"])>0,1,0)
The info table looks like this:(I´ve included the last column as that is my desired ouput)
PLAZA_CR|TZ|LOCATION|FA|TIPO_REPARTO|ACTUAL|NUEVO|**A_DO**
10GTO|8|19973|3633|DIURNO|DO|MI|**1**
10GTO|8|19975|10198|DIURNO|LUJU|DO|**0**
10GTO|8|1237|3633|DIURNO|DO|LUJU|**1**
10GTO|8|20204|3633|DIURNO|DOMAJU|LUMIJU|**1**
10GTO|8|1108|3633|DIURNO|LUMIJU|DOMAJU|**0**
10GTO|8|10895|368|DIURNO|DO|DOMIVI|**1**
10GTO|8|9434|3634|DIURNO|DOMIVI|DO|**1**
10GTO|8|17403|3633|DIURNO|DOLUMAMIJUVI|MAVI|**1**
10GTO|8|17404|3633|DIURNO|MAVI|DOLUMAMIJUVI|**0**
10GTO|8|2585|368|DIURNO|LUJU|DOMIVI|**0**
10GTO|8|16927|3634|DIURNO|DOMIVI|LUJU|**1**
Similar to as NiCE commented, you can use something like this, taking the output of grepl, which returns a logical vector for the matches:
info$A_DO <- as.numeric(grepl("DO", info[ , "ACTUAL"]))
So you don't need to use the cbind, as you can use the $ operator to create a new column of info, but you can use it if you'd prefer:
info <- cbind(info, A_DO = as.numeric(grepl("DO", info[ , "ACTUAL"])))

Paste function to construct existing data frame name and evaluate in R

I am working with a long list of data frames.
Here is a simple hypothetical example of a data frame:
DFrame<-data.frame(c(1,0),c("Yes","No"))
colnames(DFrame)<-c("ColOne","ColTwo")
I am trying to retrieve a specified column of the data frame using paste function.
get(paste("DFrame","$","ColTwo",sep=""))
The get function returns the following error, when trying to retrieve a specified column:
Error in get(paste("DFrame", "$", "ColTwo", sep = "")) :object 'DFrame$ColTwo' not found
When I enter the constructed name of the data frame DFrame$ColTwo it returns the desired output of the second column.
If I reconstruct an example without the '$' sign then I get the desired answer from the get function. For example the code yields 2:
enter code here
Ans <- 2
get(paste("An","s",sep=""))
[1] 2
I am looking for the same desired outcome, but struggling to get past the error that the object could not be found.
I also attempted using the following format, but the quotation in the column name breaks the paste function:
paste("DFrame","[,"ColTwo"]",sep="")
Thank you very much for the input,
Kind regards
You can do that using the following syntax:
get("DFrame")[,"ColTwo"]
You can use paste() in both of these strings, for example:
get(paste("D", "Frame", sep=""))[,paste("Col", "Two", sep="")]
Edit: Despite someone downvoting this answer without leaving a comment, this does exactly what the original poster asked for. If you feel that it does not or is in some way dangerous, I would encourage you to leave a comment.
Stop trying to use paste and get entirely.
The whole point of having a list (of data frames, say) is that you can reference them using names:
DFrame<-data.frame(c(1,0),c("Yes","No"))
colnames(DFrame)<-c("ColOne","ColTwo")
#A list of data frames
l <- list(DFrame,DFrame)
#The data frames in the list can have names
names(l) <- c("DF1",'DF2')
# Now you just use `[[`
> l[["DF1"]][["ColOne"]]
[1] 1 0
> l[["DF1"]][["ColTwo"]]
[1] Yes No
Levels: No Yes
If you have to, you can use paste to construct the indices passed inside [[.

How to use variable labels in R?

I have a function that takes input as the column name of dataframe as columnname~1.
The dataframe consists of about 50 columns in which I want to repeat the process, I can
use a for loop to generate column name as a character which unfortunately the function does
not recognize. The difference is just M1~1 (works) vs "M1"~1
Any suggestions are welcome
Try:
as.formula(paste("M1","1",sep="~"))
Use the "[[ function?
datafrm[[M1]] ~ 1
That function will interpret the character value and convert it to a language value. Or you could use the do.call function. Best answers will be forthcoming if we have specifics of the dataframe and the function.
Or as an alternative to paste you can use sprintf:
variablename = "M1"
as.formula(sprintf("%s ~ 1", variablename))
You can also use this:
substitute(columnname~1,list(columnname=as.name("M1")))
EDIT
It will also work when you substitute "M1" with variable containing string (M1 <- "M1").

Resources