str_remove适用于向量和数据帧,但不适用于tibble 请考虑以下三个例子: library(tidyverse) x_vector <- c("Device=iPhone", "Device=Samsung Galaxy") x_df <- as.data.frame(c("Device=iPhone", "Device=Samsung Galaxy")) x_tibble <- as_tibble(c("Device=iPhone", "Device=Samsung Galaxy")) 库(tidyverse) x_vector

str_remove适用于向量和数据帧,但不适用于tibble 请考虑以下三个例子: library(tidyverse) x_vector <- c("Device=iPhone", "Device=Samsung Galaxy") x_df <- as.data.frame(c("Device=iPhone", "Device=Samsung Galaxy")) x_tibble <- as_tibble(c("Device=iPhone", "Device=Samsung Galaxy")) 库(tidyverse) x_vector,r,tidyverse,stringr,tibble,R,Tidyverse,Stringr,Tibble,问题在于,当我们执行[,1]时,tible不会降低维度。它仍然是一个带有单个列的tible library(stringr) class(x_tibble[,1]) #[1] "tbl_df" "tbl" "data.frame" class(x_df[,1]) #[1] "factor" 相反,我们可以使用[[将列提取为向量,因为str\u remove需要向量作为输入(?str\u remove-字符串-输入向量。要么是字符向量,要么是可强制为一个的东西。)

问题在于,当我们执行
[,1]
时,
tible
不会降低维度。它仍然是一个带有单个列的
tible

library(stringr)
class(x_tibble[,1])
#[1] "tbl_df"     "tbl"        "data.frame"   

class(x_df[,1]) 
#[1] "factor"
相反,我们可以使用
[[
将列提取为向量,因为
str\u remove
需要
向量
作为输入(
?str\u remove
-
字符串
-输入向量。要么是字符向量,要么是可强制为一个的东西。)

library(stringr)
class(x_tibble[,1])
#[1] "tbl_df"     "tbl"        "data.frame"   

class(x_df[,1]) 
#[1] "factor"
str_remove(x_tibble[[1]], "Device=")