简体   繁体   English

在R中导入文本文件

[英]Import Text file in R

I have a data in a text file. 我在文本文件中有数据。 I have a problem importing it to R. The example of the text file looks like this : 我将其导入R时遇到问题。文本文件的示例如下所示:

holes:1 must:1 top_secret:1 he:1 center:1 other_civilans:1 the_pacific:1 the_navy:1 a_lot:1 surface_must:1 this_book:1 man_named:1 _feet:2 孔:1必须:1最高机密:1他:1中心:1其他公民:1 the_pacific:1 the_navy:1 a_lot:1 surface_must:1本书:1个人名:1 _feet:2

There are many lines of data like the above. 上面有很多数据行。 It would be great if anyone could help me in solving this!! 如果有人可以帮助我解决这个问题,那就太好了!!

Thanks in advance!! 提前致谢!!

I want the file to be read in two columns like 我希望在两列中读取文件,例如

holes        1  
must         1
top_secret   1
x <- data.frame('name' = scan('input_file.txt', what = 'list', sep =' '))
x$name <- as.character(x$name)
x$value <- substr(x$name, start = nchar(x$name), stop = nchar(x$name))
x$name <- substr(x$name, start = 1, stop = nchar(x$name) - 2)
print(x)
             name value
1           holes     1
2            must     1
3      top_secret     1
4              he     1
5          center     1
6  other_civilans     1
7     the_pacific     1
8        the_navy     1
9           a_lot     1
10   surface_must     1
11      this_book     1
12      man_named     1
13     <num>_feet     2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM