我有一个包含多个列的df,其值前面是" $"像这样:
> str(data)
Classes ‘data.table’ and 'data.frame': 196879 obs. of 32 variables:
$ City : chr "" "" "" "" ...
$ Company_Goal : chr "" "" "" "" ...
$ Company_Name : chr "" "" "" "" ...
$ Event_Date : chr "5/14/2016" "9/26/2015" "9/12/2015" "6/3/2017" ...
$ Event_Year : chr "FY 2016" "FY 2016" "FY 2016" "FY 2017" ...
$ Fundraising_Goal : chr "$250" "$200" "$350" "$0" ...
$ Name : chr "Heart Walk 2015-2016 St. Louis MO" "Heart Walk 2015-2016 Canton, OH" "Heart Walk 2015-2016 Dallas, TX" "FDA HW 2016-2017 Albany, NY WO-65355" ...
$ Participant_Id : chr "2323216" "2273391" "2419569" "4088558" ...
$ State : chr "" "OH" "TX" "" ...
$ Street : chr "" "" "" "" ...
$ Team_Average : chr "$176" "$123" "$306" "$47" ...
$ Team_Captain : chr "No" "No" "Yes" "No" ...
$ Team_Count : chr "7" "6" "4" "46" ...
$ Team_Id : chr "152788" "127127" "45273" "179207" ...
$ Team_Member_Goal : chr "$0" "$0" "$0" "$0" ...
$ Team_Name : chr "Team Clayton" "Cardiac Crusaders" "BIS - Team Myers" "Independent Walkers" ...
$ Team_Total_Gifts : chr "$1,230 " "$738" "$1,225 " "$2,145 " ...
$ Zip : chr "" "" "" "" ...
$ Gifts_Count : chr "2" "1" "2" "1" ...
$ Registration_Gift: chr "No" "No" "No" "No" ...
$ Participant_Gifts: chr "$236" "$218" "$225" "$0" ...
$ Personal_Gift : chr "$0" "$0" "$0" "$250" ...
$ Total_Gifts : chr "$236" "$218" "$225" "$250" ...
$ MATCH_CODE : chr "UX000" "UX000" "UX000" "UX000" ...
$ TAP_LEVEL : chr "X" "X" "X" "X" ...
$ TAP_DESC : chr "" "" "" "" ...
$ TAP_LIFED : chr "" "" "" "" ...
$ MEDAGE_CY : chr "0" "0" "0" "0" ...
$ DIVINDX_CY : chr "0" "0" "0" "0" ...
$ MEDHINC_CY : chr "0" "0" "0" "0" ...
$ MEDDI_CY : chr "0" "0" "0" "0" ...
$ MEDNW_CY : chr "0" "0" "0" "0" ...
- attr(*, ".internal.selfref")=<externalptr>
我正在尝试删除所有&#34; $&#34;。我无法这样做 - 我已经尝试了this post以及this one中提供的建议,但在这两种情况下 - 数据保持不变......
帮助?
答案 0 :(得分:2)
美元符号是正则表达式中的保留字符(有关详细信息,请参阅here)。 gsub()
函数假定pattern
默认为正则表达式。
您必须使用反斜杠(\\$
)转义美元符号以匹配文字$
。
#sample data
df = data.frame(Team_Average = c("$176", "$123", "$306"),
Name = c("Heart Walk 2015-2016 St. Louis MO",
"Heart Walk 2015-2016 Canton, OH",
"Heart Walk 2015-2016 Dallas, TX"),
stringsAsFactors = FALSE)
df[] = lapply(df, gsub, pattern="\\$", replacement="")
或者,您可以使用gsub
的{{1}}选项与字面上的fixed=TRUE
匹配。
pattern
答案 1 :(得分:0)
其他答案很好地适用于提供的示例。但是,如果数据集包含任何数字列,则通过stringr::str_replace_all()
运行lapply()
或library(stringr)
library(dplyr)
d <- data_frame(
x = c("$200", "$191.40", "80.12"),
y = c("$test", "column", "$foo"),
z = 1:3
)
d[] <- lapply(d, gsub, pattern = "\\$", replacement = "")
# A tibble: 3 x 3
x y z
<chr> <chr> <chr>
1 200 test 1
2 191.40 column 2
3 80.12 foo 3
会将数字列与字符串联:
z
请注意上面$
的课程。
以下是从所有字符列中删除d %>%
mutate_if(
is.character,
funs(str_replace_all(., "\\$", ""))
)
# A tibble: 3 x 3
x y z
<chr> <chr> <int>
1 200 test 1
2 191.40 column 2
3 80.12 foo 3
的整合方法:
public function __construct()
{
$this->middleware('auth:admin');
// $this->middleware('auth:employer');
}