在r语言中如何删除某一列数据,急急急,求详细操作

Python0499

在r语言中如何删除某一列数据,急急急,求详细操作,第1张

数据Datatest

Datatest = Datatest[,-1]就删除第一列

-2就删除第二列

依次类推

例如:

用iris数据集

data(iris)

datatest <- iris

扩展资料:

R具有很强的互动性。除了图形输出是在另外的窗口处,它的输入输出窗口都是在同一个窗口进行的,输入语法中如果出现错误会马上在窗口口中得到提示,对以前输入过的命令有记忆功能,可以随时再现、编辑修改以满足用户的需要。

输出的图形可以直接保存为JPG,BMP,PNG等图片格式,还可以直接保存为PDF文件。另外,和其他编程语言和数据库之间有很好的接口。

参考资料来源:百度百科-R语言

方法一 删除所有变量

>ls()

[1] "f" "x" "y" "z"

>rm(list=ls())

>ls()

character(0)

方法二 删除一个变量

rm()

R语言学习之数据的清理和转化

处理字符串

grep grepl 和regexpr函数都能找到与模式相匹配的字符串 sub 和 gsub函数能替换匹配的字符串

加载strngr包,fixed里面为要匹配的字符串 返回匹配的字符串序列

[plain] view plain copy

>library(stringr)

>multiple <- str_detect(english_monarchs$domain,fixed(","))

>english_monarchs[multiple,c("name","domain")]

namedomain

17 Offa East Anglia, Mercia

18 Offa East Anglia, Kent, Mercia

19 Offa and Ecgfrith East Anglia, Kent, Mercia

20 Ecgfrith East Anglia, Kent, Mercia

22C<U+009C>nwulf East Anglia, Kent, Mercia

23 C<U+009C>nwulf and Cynehelm East Anglia, Kent, Mercia

24C<U+009C>nwulf East Anglia, Kent, Mercia

25 Ceolwulf East Anglia, Kent, Mercia

26 Beornwulf East Anglia, Mercia

82 Ecgbehrt and <U+00C6>thelwulf Kent, Wessex

83 Ecgbehrt and <U+00C6>thelwulf Kent, Mercia, Wessex

84 Ecgbehrt and <U+00C6>thelwulf Kent, Wessex

85<U+00C6>thelwulf and <U+00C6>eelstan I Kent, Wessex

86 <U+00C6>thelwulf Kent, Wessex

87 <U+00C6>thelwulf and <U+00C6>eelberht III Kent, Wessex

88 <U+00C6>eelberht III Kent, Wessex

89 <U+00C6>thelred I Kent, Wessex

95 Oswiu Mercia, Northumbria

使用正则表达式来匹配多个要匹配的字符串,这是来匹配逗号和and

[plain] view plain copy

>ruler <- str_detect(english_monarchs$name,",|and")

>english_monarchs[ruler &!is.na(ruler)]

把name一列拆分掉,则可以使用str_splist函数

[plain] view plain copy

>indival <- str_split(english_monarchs$name,",|and")

>head(indival[sapply(indival,length)>1])

[[1]]

[1] "Sigeberht " " Ecgric"

[[2]]

[1] "Hun" " Beonna " " Alberht"

[[3]]

[1] "Offa " " Ecgfrith"

[[4]]

[1] "Cu009cnwulf " " Cynehelm"

[[5]]

[1] "Sighere " " Sebbi"

[[6]]

[1] "Sigeheard " " Swaefred"

st_count是用来统计有多少个字符串

[plain] view plain copy

>str_count(english_monarchs$name,th)

str_replace函数来代替字符串中的某一个

ignore.case来忽略某一个字符或字符串