当前位置:网站首页>R language [import and export of dataset]
R language [import and export of dataset]
2022-07-05 05:39:00 【桜キャンドル yuan】
Catalog
One 、 Enter data from keyboard
Four 、 Read the table data file
5、 ... and 、 Read tables from the network or CSV Data files
One 、 Enter data from keyboard
mydata <- data.frame(age=numeric(0), gender=character(0),
weight=numeric(0))
mydata <- edit(mydata)After entering our above code , We enter a text editing mode , We can use vim The syntax command of editing text to edit our data

Two 、 Function method read
1. Read data file
x<-scan(text = "1 2 3")
x
# Enter the file path to read , And store the read data as a vector
w <- scan("/Users/Documents/R/city.txt")
w
w<-scan("/Users/Documents/R/data/weight.data")
w 
2. Read data from the screen
1.scan
Read numerical data from the screen , After we input our data , We return twice in a row , You can end the input , Generate a numerical vector .
x<-scan()
x
Read string data from the screen
y <- scan(what="")
Or use the following method
2.readline
readline It can support reading a line of data input from the keyboard , Press enter to transfer the whole row of data into the variable
x=readline() 
3. Read fixed width data file
The first parameter is the full pathname of our file , The width is a vector ,w1 Represents the width of the first variable ,w2 Indicates the width of the second variable , And so on .
mydata <- read.fwf("filename",widths=c(W1,W2,...,Wn))
mydata1 <- read.fwf("/Users/Documents/R/city.txt",widths=c(4))
mydata1 
mydata<-read.fwf("/Users/Documents/R/data/FixWideData.txt",widths=c(10,10,4,-1,4)) 
We can also specify col.names Method to specify the name of our index , among -1 Parameter means to ignore the space between two years .( As we R Language entry blog ,-1 Indicates that a column of data is ignored )
mydata2<-read.fwf("/Users/Documents/R/data/FixWideData.txt",widths=c(10,10,4,-1,4),col.names=c("Last","First","Born","Died"))
The following image is our data file , We found that although our data is long and short , But it is aligned by spaces , So we can specify the width of the file to read our specific data

However, we found that such data files will have the following errors , This is because we didn't enter at the end of the last line of the data file , Our compiler can't find the sign of our end , Just add a carriage return at the end of the file , It won't be wrong

3、 ... and 、 Read csv file
# Here we will transfer our file path
mydata0<-read.csv("/Users/Documents/R/city.csv")
mydata0
# When we specify our header by F after , The column names in our original dataset are included in the data , Then a new set of indexes will be automatically generated , As shown in the figure below
mydata1<-read.csv("/Users/Documents/R/city.csv",header = F)
mydata1
as.is It is to set whether to convert character type into factor type variable
mydata2 <- read.csv("/Users/Documents/R/data/TableData.txt", as.is=F)
mydata2
mydata2 <- read.csv("/Users/Documents/R/data/TableData.txt", as.is=T)
mydata2
Four 、 Read the table data file
Each row of data is an observation
In each observation , Different variables are separated by a separator , Such as the blank space ,tab, The colon , comma
Each observation contains the same number of variables .
read.table( ).
mydata <- read.table(file, header= logical_value,sep="delimiter", rowname="name"
mydata0 <- read.table("/Users/Documents/R/data/TableData.txt") 

Of course, we can also specify our separator
mydata1 <- read.table("/Users/Documents/R/data/CommaData.txt", sep=",")
mydata1

Added stringAsFactor=False after , Data frames do not convert character types to factors
mydata2 <- read.table("/Users/Documents/R/data/TableData.txt",stringsAsFactor=FALSE)
mydata2
When we add parameters header=T in the future , The data in the first row of our original data set will become our index
mydata3 <- read.table("/Users/Documents/R/data/TableData.txt",header=T,stringsAsFactor=F)
mydata3
5、 ... and 、 Read tables from the network or CSV Data files
read.csv()
read.table()
scan()
Can get the data of the remote server
mydata0 <- read.csv("http://www.example.com/download/data.csv")
mydata1 <- read.table("ftp://ftp.example.com/download/data.csv")
边栏推荐
- 每日一题-无重复字符的最长子串
- ALU逻辑运算单元
- sync. Interpretation of mutex source code
- Sword finger offer 53 - I. find the number I in the sorted array
- Software test -- 0 sequence
- How can the Solon framework easily obtain the response time of each request?
- 剑指 Offer 04. 二维数组中的查找
- Educational Codeforces Round 116 (Rated for Div. 2) E. Arena
- Animation scoring data analysis and visualization and it industry recruitment data analysis and visualization
- Codeforces Round #732 (Div. 2) D. AquaMoon and Chess
猜你喜欢
![[article de jailhouse] jailhouse hypervisor](/img/f4/4809b236067d3007fa5835bbfe5f48.png)
[article de jailhouse] jailhouse hypervisor

Fried chicken nuggets and fifa22

Wazuh开源主机安全解决方案的简介与使用体验

剑指 Offer 06.从头到尾打印链表

Personal developed penetration testing tool Satania v1.2 update

剑指 Offer 05. 替换空格

Gbase database helps the development of digital finance in the Bay Area
![[jailhouse article] jailhouse hypervisor](/img/f4/4809b236067d3007fa5835bbfe5f48.png)
[jailhouse article] jailhouse hypervisor

【Jailhouse 文章】Look Mum, no VM Exits

Yolov5 adds attention mechanism
随机推荐
动漫评分数据分析与可视化 与 IT行业招聘数据分析与可视化
2020ccpc Qinhuangdao J - Kingdom's power
Scope of inline symbol
sync. Interpretation of mutex source code
Solution to the palindrome string (Luogu p5041 haoi2009)
Introduction to convolutional neural network
Sword finger offer 35 Replication of complex linked list
Haut OJ 2021 freshmen week II reflection summary
sync.Mutex源码解读
SAP method of modifying system table data
Control Unit 控制部件
【Jailhouse 文章】Jailhouse Hypervisor
YOLOv5-Shufflenetv2
SAP-修改系统表数据的方法
ALU逻辑运算单元
SSH password free login settings and use scripts to SSH login and execute instructions
Simple knapsack, queue and stack with deque
Codeforces Round #716 (Div. 2) D. Cut and Stick
YOLOv5添加注意力機制
Kubedm series-00-overview