Home > OS >  Create a new column is returning as list
Create a new column is returning as list

Time:10-12

I have a dataframe. I need to remove "href" and hence I am using str_match_all. But after removing "href", the class of new column is showing as list. Can anyone help me

asd <- data.frame(a = c("<a href=https://abc>Click Here</a>","sdsd","<a href=https://xvd>Click Here</a>"))
asd$ew <- str_match_all(asd$a, "http[s]{0,1}://[^ ] ")
asd
                                   a                ew
<a href=https://abc>Click Here</a>        https://abc>Click
                              sdsd                  
<a href=https://xvd>Click Here</a>        https://xvd>Click
class(asd$ew)
"list"

Expected output

class(asd$ew)
 "character"

CodePudding user response:

Use stringr::str_extract instead of stringr::str_match_all.

asd$ew <- stringr::str_extract(asd$a, "http[s]{0,1}://[^ ] ")
class(asd$ew)
#[1] "character"

asd
#                                   a                ew
#1 <a href=https://abc>Click Here</a> https://abc>Click
#2                               sdsd              <NA>
#3 <a href=https://xvd>Click Here</a> https://xvd>Click

CodePudding user response:

Use as.character:

> asd$ew <- as.character(str_match_all(asd$a, "http[s]{0,1}://[^ ] "))
> class(asd$ew)
[1] "character"
> 
  • Related