Error while importing minitab file into R - r

I was using the foreign package in R to import the MTP file, but it only shows file read error. I already set up the default directory, but it does not work. I would really appreciate if I could get some help. Thank you in advance.
library(foreign)
d44<-read.mtp("PERAGGR.MTP")
Here is the file that I was using

The read.mtp function is fairly ancient. And since that file is actually an ASCII file, just read it with `readLines:
d44<-readLines("~/Downloads/PERAGGR.MTP")
head(d44)
# ============== top of output==================
[1] "Minitab Portable Worksheet P1.1 2007 523 Rel 15.0 ."
[2] "% 3 1 109 0 Study ."
[3] " 1.000000000E+00 2.000000000E+00 3.000000000E+00 4.000000000E+00 5.000000000E+00"
[4] " 6.000000000E+00 7.000000000E+00 8.000000000E+00 9.000000000E+00 1.000000000E+01"
[5] " 1.100000000E+01 1.200000000E+01 1.300000000E+01 1.400000000E+01 1.500000000E+01"
[6] " 1.600000000E+01 1.700000000E+01 1.800000000E+01 1.900000000E+01 2.000000000E+01"

Related

Understanding Createview in SQLITE3

I am trying to understand CREATE VIEW command in SQLite, but getting strange view.
I have a CSV file,
table.csv / tab separated
ID NAME AGE ADDRESS SALARY
1 Paul 32 California 20000.0
2 Allen 25 Texas 15000.0
3 Teddy 23 Norway 20000.0
4 Mark 25 Rich-Mond 65000.0
5 David 27 Texas 85000.0
6 Kim 22 South-Hall 45000.0
7 James 24 Houston 10000.0
run commands*
sqlite3 sample.db
.mode csv
.import table.csv COMPANY
CREATE VIEW COMPANY_VIEW AS
SELECT "ID", "NAME", "AGE"
FROM COMPANY;
SELECT * FROM COMPANY_VIEW;
getting
"1 ",NAME,"32 "
"2 ",NAME,"25 "
"3 ",NAME,"23 "
"4 ",NAME,"25 "
"5 ",NAME,"27 "
"6 ",NAME,"22 "
"7 ",NAME,"24 "
Why I am not getting NAME Column. What I am doing wrong. I am an absolute beginner in SQLITE3.
Suggest issuing .schema COMPANY and/or SELECT * FROM COMPANY before the CREATE VIEW command.
From the sqlite doc
Use the ".import" command to import CSV (comma separated value) data into an SQLite table.
The operative term here is comma separated.
Use the .separator command to change the separator to tab.

R programming encoding jebrish ׳‘׳“׳™׳§׳•׳× to Hebrew

I have some Hebrew string along my code.
For some reason all the Hebrew is shown in the following format ׳§׳‘׳•׳¦׳×.
I have this line at the top of every file
Sys.setlocale("LC_ALL", "Hebrew")
This is the output of the commend Sys.getlocale("LC_ALL")
"LC_COLLATE=Hebrew_Israel.1255;LC_CTYPE=Hebrew_Israel.1255;LC_MONETARY=Hebrew_Israel.1255;LC_NUMERIC=C;LC_TIME=Hebrew_Israel.1255"
Example:
hebrew <- c(
"סוג תנועה",
"מס עוסק נגדי",
"תאריך אסמכתא",
"קבוצת אסמכתא",
"מס אסמכתא",
"סכום מעמ",
"סימן",
"סכום לפני מעמ",
" עתידי",
"הערה",
"בדיקות",
"סכום.מעמ",
"סכום.לפני.מעמ",
"הערות"
)
print(hebrew)
Result:
׳¡׳•׳’ ׳×׳ ׳•׳¢׳”" "[1] "\u009e׳¡ ׳¢׳•׳¡׳§ ׳ ׳’׳“׳™" "׳×׳\u0090׳¨׳™׳\u009a
׳\u0090׳¡׳\u009e׳›׳×׳\u0090" "׳§׳‘׳•׳¦׳× ׳\u0090׳¡׳\u009e׳›׳×׳\u0090" "׳\u009e׳¡
׳\u0090׳¡׳\u009e׳›׳×׳\u0090" "׳¡׳›׳•׳\u009d ׳\u009e׳¢׳\u009e"
[7] "׳¡׳™׳\u009e׳\u009f" "׳¡׳›׳•׳\u009d ׳\u009c׳₪׳ ׳™ ׳\u009e׳¢׳\u009e" "
׳¢׳×׳™׳“׳™" "׳”׳¢׳¨׳”" "׳‘׳“׳™׳§׳•׳× "׳¡׳›׳•׳\u009
Move the function to the main file

How to fix a problem with Cyrillic symbols (cmd system commands)?

In my previous post, I brought up a question about Cyrillic symbols in R.
Today I have faced another one.
For example, we want to see our running processes:
test <- system2(command="tasklist",
stdout=TRUE,
stderr=TRUE,
wait = TRUE)
and what we see...
[1] ""
[2] "€¬п ®Ўа § PID €¬п бҐббЁЁ ь ᥠ­б Џ ¬пвм"
[3] "========================= ======== ================ =========== ============"
[4] "System Idle Process 0 Services 0 24 ЉЃ"
[5] "System 4 Services 0 580 ЉЃ"
***
"Iconv", which helped in the previous task - couldn't help here.
sys.setlocale - too.
What can solve this problem?
I found a solution.
#/c - Carries out the command specified by string and then stops.
command <- function(command,
intern = TRUE,
wait = FALSE)
system(paste("cmd.exe /c", command),
intern = T,
wait = wait)
#changing our charset
command("chcp 1251")
[1] "’ҐЄгй п Є®¤®ў п бва ­Ёж : 1251" //say bye-bye to mojibake)
# and voila!
command("tasklist")
[1] ""
[2] "Имя образа PID Имя сессии № сеанса Память"
[3] "========================= ======== ================ =========== ============"
[4] "System Idle Process 0 Services 0 24 КБ"
[5] "System 4 Services 0 580 КБ"
[6] "smss.exe 380 Services 0 1 232 КБ"
***

Arranging text lines in R

My data is in this format. It's a text file and the class is "character". I have posted few lines from the file. There are about 14000 lines.
"KEY: Aback"
"SYN: Backwards, rearwards, aft, abaft, astern, behind, back."
"ANT: Onwards, forwards, ahead, before, afront, beyond, afore."
"KEY: Abandon"
"SYN: Leave, forsake, desert, renounce, cease, relinquish,"
"discontinue, castoff, resign, retire, quit, forego, forswear,"
"depart_from, vacate, surrender, abjure, repudiate."
"ANT: Pursue, prosecute, undertake, seek, court, cherish, favor,"
"protect, claim, maintain, defend, advocate, retain, support, uphold,"
"occupy, haunt, hold, assert, vindicate, keep."
Line 6 and 7 is the continuation of line 5. Line 9 and 10 is the continuation of line 8. My struggle is how can I bring up line 6 and 7 to line 5 and similarly line 9 and 10 to line 8.
Any hints gratefully received.
First thing that comes to mind (your text is stored as x):
#prefix each line starter (identifies as pattern: `CAPS:`) with a newline (\n)
strsplit(gsub("([A-Z]+:)", "\n\\1", paste(x, collapse = " ")),
split = "\n")[[1L]][-1L]
# [1] "KEY: Aback "
# [2] "SYN: Backwards, rearwards, aft, abaft, astern, behind, back. "
# [3] "ANT: Onwards, forwards, ahead, before, afront, beyond, afore. "
# [4] "KEY: Abandon "
# [5] "SYN: Leave, forsake, desert, renounce, cease, relinquish, discontinue, castoff, resign, retire, quit, forego, forswear, depart_from, vacate, surrender, abjure, repudiate. "
# [6] "ANT: Pursue, prosecute, undertake, seek, court, cherish, favor, protect, claim, maintain, defend, advocate, retain, support, uphold, occupy, haunt, hold, assert, vindicate, keep."

find the number of lines in a file

Under Linux, I can find the number of lines in a file by doing a system call to wc:
CountLines <- function(file) {
count.file <- system(sprintf("wc -l %s", file), intern = TRUE)
count <- as.integer(strsplit(count.file, " ")[[1]][1])
return(count)
}
How can I do this efficiently under Windows? By "efficient" I mean fast and light on resources, as I may be using it on large files.
As much as possible, I'd prefer a solution that does not require installing extra packages or tools.
Take a look at this link:
https://isc.sans.edu/diary/Finding+Files+and+Counting+Lines+at+the+Windows+Command+Prompt/2244
The last line works for me:
c:\> type c:\windows\win.ini | find /c /v "~~~"
# 32
Update:
if you want to use it as R function, try this:
CountLines <- function(file) {
stopifnot(file.exists(file))
unlikely.pattern <- paste(sample(LETTERS), collapse = "")
cmd <- sprintf('type %s | find /c /v "%s"', file, unlikely.pattern)
res <- shell(cmd, intern = TRUE)
return(as.integer(res))
}
CountLines("c:\\windows\\win.ini")
[1] 32
.
.
.
I found another way to do this more efficient, but I leave this to you for perfection:
> system("POWERSHELL Get-Content c:\\windows\\win.ini | Measure-Object -word -line -character", intern=TRUE)
[1] ""
[2] " Lines Words Characters Property "
[3] " ----- ----- ---------- -------- "
[4] " 32 38 414 "
[5] ""
[6] ""

Resources