Array of character with non-printable characters in Julia - julia

I want to create an array of chars where some of the values are non-printable characters but not spaces:
I tried:
a = ['a', 'b', \n]
a = ['a', 'b', '']
a = ['a', 'b', nothing]
without success.

Related

Is there a way to quickly encase a list of words with "quotation marks"

I am trying to create a vector of words in R, but have a huge list of words, so manually writing "word1", "word2", ..., "word100" would be very tedious.
Given I have the list of words comma separated, is there a way to quickly encase them with quotation marks?
For instance I have:
words = c(apple,bannana,cat,...,dolphin)
but I want
words = c("apple","bannana","cat",...,"dolphin").
If we want to create a character vector from an expression
words <- scan(text = trimws(str1, whitespace = ".*\\(|\\)"),
what = "", sep = ",", quiet = TRUE)
data
str1 <- "words = c(apple,bannana,cat)"
Assuming you have the list on your clipboard:
wordlist <- unname(unlist(read.csv(file="clipboard", header=FALSE))) # Windows
wordlist <- unname(unlist(read.csv(pipe("pbpaste"), header=FALSE))) $ MacOs
wordlist
# [1] "apple" "bannana" "cat" "dolphin"
You may get a warning message if there is no line return at the end of the clipboard listing, but you can ignore it.

Naming mutliple xlsx files with TRUE of FALSE if character string is present in a particular sheet

This code reads a xlsx file and creates individualy named files based on sheet number and a value found at a particular location (in this case temp[2,1]). However because each file and sheet is slightly different the names are inconsistant.
sheet_to_read <- c(11,12,13,14)
for( excelsheet in files) {
for (sheet in sheet_to_read) {
temp <- read_excel( path = excelsheet, sheet = sheet, col_names = FALSE)
write.csv( temp, file = paste0( "./", gsub("./", "", excelsheet), temp[2,1], sheet,".csv") )
}}
I would like is a way of naming the files with TRUE or FALSE if a specific character string is present any where within the sheet, in this case 'vivax'. So in this case:
GBD2016_2_915_Boletin Epidemiologico_2016_37sheet21true.xls
GBD2016_2_915_Boletin Epidemiologico_2016_37sheet22false.xls`
Example file: https://drive.google.com/file/d/1p4HAuFl7Codine1Vvb8SzA7OHTzraaHz/view?usp=sharing
Since you have a tibble, and it is not known which column has which type,
I created this:
isWordInTibble <- function(word, tibble) {
# case insensitive
any(unlist(
sapply(1:ncol(tibble),
function(i) tolower(word) %in% tolower(as.character(unlist(tibble[, i]))))))
}
It looks whether the word is in any of the column vectors - looks through all columns.
Replace your file argument in your write.csv command by:
file = gsub(".xls",
paste0(substr(temp[2, 1],
1,
5), # just first 5 letters
gsub("sheet", "", sheet),
substr(tolower(as.character(isWordInTibble("vivax", tmp))),
1,
1), # just first letter ("t" or "f")
".csv"),
excelsheet)
Then it might work!
I tried to shorten the names using substr(, start, end) and gsub().
Appendix
Since you asked how to print then only the files containing the word but not those which don't contain it:
In your example, instead of the write.csv() command, replace it with:
if (isWordInTibble("vivax", tmP)) {
write.csv(temp,
file = gsub(".xls",
paste0(substr(temp[2, 1],
1,
5), # just first 5 letters
gsub("sheet", "", sheet),
substr(tolower(as.character(isWordInTibble("vivax", temp))),
1,
1), # just first letter ("t" or "f")
".csv"),
excelsheet))
}
Then it prints out only if the isWordInTibble returns TRUE.

Trying to create a loop to export excel files from a list of data

I have a list of data (150 student names + class + grade) and I am trying to create Excel sheets by class. I currently have a list of data broken up by class, but I am not able to export them into Excel.
IncomingClasses <- split(Students, Students$Class-Course)
classes <- names(IncomingClasses)
for (i in seq(classes)){
assign(classes[i], IncomingClasses[[i]])
write.xlsx(IncomingClasses[i], file = paste(i, ".xlsx"))
}
But I'm not able to export. The best error message I've gotten is:
Error in (function (..., row.names = NULL, check.rows = FALSE, check.names = TRUE, : arguments imply differing number of rows: 34,
44, 15, 17, 16, 18
Because I'm supposed to have different rows as there are different students in each class...but it won't create the .xlsx document.
Please help!
Let's say I've got this data frame, initialised with these variables.
student <- c('joe', 'bob', 'foo', 'bar')
class <- c('1', '2', '3', '4')
grade <- c('a', 'b', 'c', 'd')
df <- data.frame(student, class,
Why not create a temporary data frame and save that to file, with the appropriate subset to entries with the correct class designation?
for (c in unique(df$class)) {
tdf <- df[df$class == c, ]
write.csv(tdf, file = paste(c, ".csv"))
}
You don't provide a fully reproducible example of the problem, so it's hard to diagnose, but I see at least three problems. To access the split-up data frames, you should use [[, not [, your use of seq will produce numbered file names, and you should use paste0 instead of paste to avoid inserting a space in the file names. Additionally, the use of assign is confusing. Might I suggest:
for (i in names(IncomingClasses)) {
write.xlsx(IncomingClasses[[i]], file = paste0(i, '.xlsx'))
}

numeric fields turning into "char" while using stringsAsFactor = F

I am trying to import a few csv files from a specific folder:
setwd("C://Users//XYZ//Test")
filelist = list.files(pattern = ".*.csv")
datalist = lapply(filelist, FUN=read.delim, sep = ',', header=TRUE,
stringsAsFactors = F)
for (i in 1:length(datalist)){
datalist[[i]]<-cbind(datalist[[i]],filelist[i])
}
Data = do.call("rbind", datalist)
After I use the above code, a few columns are type character, despite containing numbers. If I don't use stringsAsFactor = F then the fields read as factor which turns into missing values when I use as.numeric(as.character()) later on.
Is there any solution so that I can keep some fields as numeric? The fields that I want to be as numeric look like this:
Price.Plan Feature.Charges
$180.00 $6,307.56
$180.00 $5,431.25
Thanks
The $, , are not considered numeric, so while using stringsAsFactors = FALSE in the read.delim, it assigns the column type as character. To change that, remove the $, , with gsub, convert to numeric and assign it to the particular columns
df <- lapply(df, function(x) as.numeric(gsub("[$,]", "", x)))

How to avoid space while reading .txt file

I am trying to read a file which has a column of + and - but apparently associated with a space after that. In anyway I read (read.csv or read.table or read_excel that file I pick up the space after.
For example:
df = data.frame(x = c('a', 'b', 'c'), y = c('+ ', "- ", "+ "))
df
x y
1 a +
2 b -
3 c +
Now, is there a way to specifically instruct not to pick up space after the value in read.csv or read_excel
Or is there a way to clean up the spaces after reading into a dataframe?

Resources