0

formula_df is a dataframe that looks like so:

> formula_df
# A tibble: 4 × 4
  lhs_new       rhs_new                          f_new                                       type  
  <chr>         <chr>                            <chr>                                       <chr> 
1 A_rejected    D_B+D_C+D_D+G_male+Q_unqualified A_rejected~D_B+D_C+D_D+G_male+Q_unqualified binary
2 D             G_male                           D~G_male                                    multi 
3 G_male        1                                G_male~1                                    binary
4 Q_unqualified 1                                Q_unqualified~1                             binary
x <- formula_df[2,'lhs_new']

# A tibble: 1 × 1
  lhs_new
  <chr>  
1 D   

How do I convert this to a string in R?

I tried:

str(x)

but when i call the folowing i get this:

class(str(x))

tibble [1 × 1] (S3: tbl_df/tbl/data.frame)
 $ lhs_new: chr "D"
[1] "NULL"
8
  • @akrun This is just base R Commented Aug 31, 2021 at 19:26
  • Sorry, i was thinking about formula_df Commented Aug 31, 2021 at 19:26
  • str in R is a utility function to view the structure of an object, do not confuse it with python's str function that tries to convert an object to a string. Commented Aug 31, 2021 at 19:27
  • The lhs_new column is already of type character (i.e., a string). What do you expect the output of this to be? Commented Aug 31, 2021 at 19:27
  • perhaps like quote(, it is unclear what part you want as string., Commented Aug 31, 2021 at 19:27

2 Answers 2

1

In base R, a data.frame that is indexed on both the row and column will return just the element. In R parlance, this means it "drops" the dimensions. A tibble, on the other hand, will not drop and will "always" be a frame until you force it in other ways.

For example,

formula_df[2,"lhs_new"]
# # A tibble: 1 x 1
#   lhs_new
#   <chr>  
# 1 D      
as.data.frame(formula_df)[2,"lhs_new"]
# [1] "D"

To get the same "do not drop" behavior in R, one would do

as.data.frame(formula_df)[2,"lhs_new",drop=FALSE]
#   lhs_new
# 2       D

Not that you want that, but I present it for perspective. Bottom line, you need to either convert to data.frame (not tibble) and subset, or use an alternate form of indexing, including

formula_df[["lhs_new"]][2]
# [1] "D"

(as akrun just demonstrated).

Sign up to request clarification or add additional context in comments.

Comments

1

The classshould be on the extracted column

class(formula_df$lhs_new)

As it is a tibble, we need either $ or [[ as the [ wouldn't drop the dimensions as in data.frame where the default option is drop = TRUE according to ?Extract

x[i, j, ... , drop = TRUE]

drop - logical. If TRUE the result is coerced to the lowest possible dimension. The default is to drop if only one column is left, but not to drop if only one row is left.

formula_df[['lhs_new']][2]

If we check the methods for

methods("[")

it returns a vector of methods, around 150 or so and [.tbl_df is one of them. By searching on ?"[.tbl_df", it can give more info

Accessing columns, rows, or cells via $, [[, or [ is mostly similar to regular data frames. However, the behavior is different for tibbles and data frames in some cases:

[ always returns a tibble by default, even if only one column is accessed.


Also, wrapping with str and assigning to an object doesn't have any effect as it is just printing to console. According to ?str

str does not return anything, for efficiency reasons. The obvious side effect is output to the terminal.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.