Display Exact Value of a Variable in R

Display exact value of a variable in R

Globally solution during all the session

options(digits=16)
> x
[1] 1.00042589212565

or locally just for x:

sprintf("%.16f", x)
[1] "1.0004258921256499"

How to check for the existence of a certain value in a set of variables only when there is no NA?

here is another workaround by transposing the data.frame and an apply on colonns. I'm not sure it's more elegant but here it is ^^

tmp <- cbind(sub("^((Happy)|(Sad))(_.*_Num)$", "\\1", colnames(df)), t(df))
Happy_Any4 <- apply(tmp[tmp[,1]== "Happy", -1], 2,
function(x) ifelse(any(is.na(x)), NA, length(grep("4", x))) )
Sad_Any4 <- apply(tmp[tmp[,1]== "Sad", -1], 2,
function(x) ifelse(any(is.na(x)), NA, length(grep("4", x))) )

df <- cbind(df, Happy_Any4 = Happy_Any4, Sad_Any4 = Sad_Any4)

EDIT : Above was a strange test, but now this work with more beauty !

This is because the sum of anything where there is an NA will return NA.

df <- df %>% mutate(Happy_Any4 = apply(df[,grep("^Happy_.*_Num$", colnames(df))], 
1, function(x) 1*(sum(x == 4) > 0)),
Sad_Any4 = apply(df[, grep("^Sad_.*_Num$", colnames(df))],
1, function(x) 1*(sum(x == 4) > 0)))

The apply will look every row, only on columns where we find the correct part in colnames (with grep. It then find every occurence of 4, which form a logical vector, and it's sum is the number of occurence. The presence of an NA will bring the sum to NA. I then just check if the sum is above 0 and the 1* will turn the numeric into logical.

Extract specific value from a string variable in r

Try this

library(dplyr)
df %>% mutate(character = sub(".+title=\"(.+)\".+", "\\1", character))

Getting a specific value of data.table using a variable in R

Per @jogo's comment, you're using a data.table.

The solution is in the error:

Perhaps you intended DT[, ..x]

df[2, ..x] should do the trick.

Get exact values of floats in R?

That's just R's default printing limit of 7 significant figures. To see the true underlying values:

print(unique(c(0.100000000002, 0.100000000003), digits=15)

To change the default behaviour, see ?options; you want something like options(digits=15).

R: Create Variable and Display Value in Single Command

Wrap the command in parantheses.

(x <- mean(1:7))

Knowing this you can do a few cool R tricks like creating an object and using it in the same expression. The development team does this all over the R source code. This is a double nest from apply:

if (length(dn.call) && !is.null(n1 <- names(dn <- dn.call[1])) && 
nzchar(n1) && length(ans.names) == length(dn[[1]]))

Be careful with these newfound powers, your co-workers will never be able to read your code again :)

how to display data frame variable value in a string of another dataframe in r?

Try glue package.

  • One more thing, either use " inside ' ' or ' inside "", but don't mix these.
  • Use either
    • df[df$ID=="1234","Name"] bought the expensive product df[df$ID=="1234","price"]
    • OR
    • df[df$ID=='1234','Name] bought the expensive product df[df$ID=='1234','price']
    • but don't use
    • df[df$ID=="1234",'Name'] bought the expensive product df[df$ID=="1234",'price']
library(glue)

df <- read.table(text = 'ID Name price
1234 Rick 333
4568 Jim 555
231 Rex 122', header = T)

glue('{df[df$ID=="1234","Name"]} bought the expensive product {df[df$ID=="1234", "price"]}')
#> Rick bought the expensive product 333

Created on 2021-05-26 by the reprex package (v2.0.0)



Related Topics



Leave a reply



Submit