Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 基于列中数值的子集数据表_R_Subset - Fatal编程技术网

R 基于列中数值的子集数据表

R 基于列中数值的子集数据表,r,subset,R,Subset,这是我的第一个堆栈溢出后,所以请与我忍受 我希望在R中子集一个数据表,以便只包括包含数字的行,而排除包含字符的行。比如桌子 Date Temperature 41941.6656 1921 41941.6656 1921 41941.66561 1921 41941.66563 1921 41941.66564 1921 41941.pypito 1921 41941.66566 xWRET 41941.66567

这是我的第一个堆栈溢出后,所以请与我忍受

我希望在R中子集一个数据表,以便只包括包含数字的行,而排除包含字符的行。比如桌子

 Date   Temperature
    41941.6656  1921
    41941.6656  1921
    41941.66561 1921
    41941.66563 1921
    41941.66564 1921
    41941.pypito    1921
    41941.66566 xWRET
    41941.66567 1921
应该成为

Date    Temperature
41941.6656  1921
41941.6656  1921
41941.66561 1921
41941.66563 1921
41941.66564 1921
41941.66567 1921
其中,xWRET表示我的愚蠢工具经常使用的一些随机字符串

我试过
is.numeric()
grep()
,但是我都不能工作

我觉得这应该很简单


谢谢

如果您还不熟悉它的习惯用法及其相对复杂性,那么也可以使用基函数

假设您将data.frame列作为字符

df$Date <- as.numeric(df$Date)
df$Temperature <- as.numeric(df$Temperature)
得到

         Date Temperature
[1,] 41941.67        1921
[2,] 41941.67        1921
[3,] 41941.67        1921
[4,] 41941.67        1921
[5,] 41941.67        1921
[6,] 41941.67        1921

更多关于
数据的介绍。表

这肯定不是最好的方法,但它是这样的:

library(data.table) # use this package, it is great for perfomance
lines="
41941.6656 1921
41941.6656 1921
41941.66561 1921
41941.66563 1921
41941.66564 1921
41941.pypito 1921
41941.66566 xWRET
41941.66567 1921"
con <- textConnection(lines)
d = data.table(read.table(con,stringsAsFactors = FALSE,
           sep=" ", 
           col.names=c("Date", "Temperature"), 
           fill=FALSE, 
           strip.white=TRUE))
close(con)
d<-d[!is.na(as.numeric(Temperature)) & !is.na(as.numeric(substr(Date,start=7,stop=7)))]

这就是您想要的吗?

我认为
数据.table
的更普遍、更有效的使用方法是使用
.SD
lapply
(如果您想转换两个以上的列,而不想手动指定它们)

或者

library(data.table)
na.omit(setDT(df)[, lapply(.SD, function(x) as.numeric(as.character(x)))])
#        Date Temperature
# 1: 41941.67        1921
# 2: 41941.67        1921
# 3: 41941.67        1921
# 4: 41941.67        1921
# 5: 41941.67        1921
# 6: 41941.67        1921


日期列被认为是数字吗?是的-我可以很容易地转换日期。然而,我认为我首先需要清除所有的字符条目,因为它们不应该存在!如果
dt
是您的
data.table
,您可以尝试
dt[!(grepl([a-zA-Z]”,Date)| grepl([a-zA-Z]”,Temperature)),]
。或者,您可以
dt[]感谢nicola-效果很好!:)这里假设列是字符类型的,对吗?另外,我不确定是否有你提到的
数据表
@DavidArenburg,谢谢你的评论。是的,假设是char类型,我已经添加了对
data.table
的引用。
library(data.table) # use this package, it is great for perfomance
lines="
41941.6656 1921
41941.6656 1921
41941.66561 1921
41941.66563 1921
41941.66564 1921
41941.pypito 1921
41941.66566 xWRET
41941.66567 1921"
con <- textConnection(lines)
d = data.table(read.table(con,stringsAsFactors = FALSE,
           sep=" ", 
           col.names=c("Date", "Temperature"), 
           fill=FALSE, 
           strip.white=TRUE))
close(con)
d<-d[!is.na(as.numeric(Temperature)) & !is.na(as.numeric(substr(Date,start=7,stop=7)))]
          Date Temperature
1:  41941.6656        1921
2:  41941.6656        1921
3: 41941.66561        1921
4: 41941.66563        1921
5: 41941.66564        1921
6: 41941.66567        1921
library(data.table)
na.omit(setDT(df)[, lapply(.SD, function(x) as.numeric(as.character(x)))])
#        Date Temperature
# 1: 41941.67        1921
# 2: 41941.67        1921
# 3: 41941.67        1921
# 4: 41941.67        1921
# 5: 41941.67        1921
# 6: 41941.67        1921
setDT(df)[, names(df) := lapply(.SD, function(x) as.numeric(as.character(x)))][complete.cases(df)]
#        Date Temperature
# 1: 41941.67        1921
# 2: 41941.67        1921
# 3: 41941.67        1921
# 4: 41941.67        1921
# 5: 41941.67        1921
# 6: 41941.67        1921