我很抱歉,因为肯定有很多类似的问题和答案,但我已经try 了一堆建议的答案,遗憾的是没有.
我在一个数据帧(Tempdata)的三列中获得了温度数据.为简单起见,我只是try 一次更改其中一个位置(wentworth.Castle).
这就是我的数据.所有带有".Castle"的柱子都是该站点的温度.缺少值,但这是意料之中的.希望把它们变成NA.
glimpse(tempdata)
Rows: 3,395
Columns: 5
$ Description <chr> "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", …
$ date.time <chr> "22/11/2023 09:48", "22/11/2023 10:18", "22/11/2023 10:48", "22/11/2023 11:…
$ site.castle <chr> "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",…
$ dover.castle <chr> "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",…
$ wentworth.castle <chr> "9.484 \xb0C", "9.642 \xb0C", "9.768 \xb0C", "9.994 \xb0C", "10.066 \xb0C",…
我已经try 了下面的几种方法,得到了以下错误.
tempdata$wentworth.castle <- gsub(" �C", "", as.numeric(tempdata$wentworth.castle))
#Error in is.factor(x) : invalid multibyte string at '<b0>C'
tempdata$wentworth.castle <- gsub(" \xb0C", "", as.numeric(tempdata$wentworth.castle))
#Error in is.factor(x) : invalid multibyte string at '<b0>C'
tempdata$wentworth.castle = tempdata$wentworth.castle.replace('\u00b0','', regex=True)
#Error: attempt to apply non-function
tempdata$wentworth.castle <- as.numeric(tempdata$wentworth.castle)
#Error: invalid multibyte string at '<b0>C'
我还try 了一种不太健壮的方法,并try 创建一个函数来删除特定数量的字符后的内容,但是这很困难,因为有时我的数据有5个符号数字,有时有6个,所以即使它起作用了,我也会从一些条目中删除一些随机空格.
left = function(string, chat){substr(string, 1, char)}
tempdata$wentworth.castle <- left(tempdata$wentworth.castle, 6)
#Error in as.integer(stop) :
# cannot coerce type 'closure' to vector of type 'integer'