toString of HH:MM in R - r

Having a Dataframe with "15:15" on dataframe[14,3], when I do a toString it prints:
911
What should be the problem here? If I print dataframe[14,3], it correctly prints 15:15.
I am trying to paste three variables and one of them, being in this format, it is appearing as a whole number (which I do not understand the relation with the original string).

The problem would be based on the class of the column. If it is a factor class, then by doing the toString, could change the class to integer storage mode. The option would be to convert it to character and then apply the function
dataframe[,3] <- as.character(dataframe[,3])

Related

Two PASTE functions in a character vector

attach.files = c(paste("/users/joesmith/nosection_", currentDate,".csv",sep=""),
paste("/users/joesmith/withsection_", currentDate,".csv",sep=""))
Basically, if I did it like
c("nosection_051418.csv", "withsection_051418.csv")
And I did that manually it would work fine but since I'm automating this to run every day I can't do that.
I'm trying to attach files in an automated email but when I structure it like this, it doesn't work. How can I recreate this so that the character vector accepts it?
I thought your example implied the need for "parallel" inputs to the path stem, the first portion of the file name, and the date portions of those full paths. Consider this illustration of using a 2 item vector and a one item vector (produced by Sys.Date, replacing your "currentdate") to populate the %s positions in that sprintf string (suggested by #Gregor):
sprintf("/users/joesmith/%s_%s.csv", c("nosection", "withsection"), Sys.Date() )
[1] "/users/joesmith/nosection_2018-05-14.csv" "/users/joesmith/withsection_2018-05-14.csv"

First variable replacing others

I have the following code:
from a3_functions import convert_date
date=int(input('Please enter the date in the format: ddmmyyyy'))
days, months, years=convert_date(date)
print("{0:02d}/{0:02d}/{0:04d}".format(days, months, years))
print(days)
print(months)
print(years)
But when I run it outputs the following:
Please enter the date in the format: ddmmyyyy03061314
03/03/0003
3
6
1314
Why does the sentence form of my output just output the first variable three times? Even though when I print them individually they print their respective values.
Your format string is using positional indices (which all are 0 and thus taking the first element).
Try this format string instead (I basically only removed all the 0 in front of the :):
>>> print("{:02d}/{:02d}/{:04d}".format(days, months, years))
03/06/1314
If you want to keep the positional arguments, you could just increment the numbers like this:
>>> print("{0:02d}/{1:02d}/{2:04d}".format(days, months, years))
03/06/1314
Alternatively, you could also name the positions to use an even more readable format string:
>>> print("{day:02d}/{month:02d}/{year:04d}".format(day=days, month=months, year=years))
03/06/1314
PyFormat has a great overview over the new-style format strings:
With new style formatting it is possible (and in Python 2.6 even
mandatory) to give placeholders an explicit positional index.
This allows for re-arranging the order of display without changing the
arguments.
This operation is not available with old-style formatting.
New: '{1} {0}'.format('one', 'two')
Output: two one

How to create/implement a R script in Spotfire to add the commas to a converted string number?

In a Spotfire report, after a REAL number converted to a string, the commas got lost. How to create and implement an R script to add the commas in the converted number?
You don't to use R for this. You should convert it back either in the presentation of your analytic, or create a calculated column and change the formatting.
Insert > Calculated Column: Real([YourStringColumn])
Edit > Column Properties > YourNewColumn > Formatting > Number: Check Use thousands separator
Of note, I'm interested in the following
How did a Real number get converted to a String and
Why did this conversion drop the commas? I'd think it would keep them.

Calling a data.frame from string

I am doing a loop and I need to subset a fixed range of the columns of a data frame. But the loop is exactly to generate the name of the data.frame that I need to extract the columns. I would like to know how can I call a data.frame from a string name. It must be something similar to assign() function, but I am not assigning any value to nothing, I just need to generate the name of a data.frame from a string using paste0() function.
a=5
b="a"
get(b)
Output:
5
How I found this? I did help(assign) and looked under "See also" section.

How to fix a character value returned as a numeric value in R?

I am a new R user and having some difficulty when trying to rename certain records in a column.
My data have columns named classcode and fish_tl, among others. Classcode is a character value, fish_tl is numeric.
When classcode='OCAL' and fish_tl<20, I need to rename that value of classcode so that it is now "OCALYOY". I don't want to change any of the other records in classcode.
I'm running the following code:
data$classcode<-ifelse(data$classcode=='OCAL'& data$fish_tl<20,
'OCALYOY',data$classcode)
My problem seems to be with the "else" aspect: the code runs fine, and returns 'OCALYOY' as expected, but the other values of classcode have now been converted to numeric (although when I look at the mode of that field, it still returns as "character").
What am I doing wrong?
Thanks very much!
You can make the else part as.character(data$classcode). ifelse has some odd semantics with regard to the classes of the arguments, and it is turning your factor into it's underlying numeric representation. as.character will keep it as a character value.
You may be getting tripped up in a factor vs character issue, though you point out that R thinks it's character. Regardless, wrapping as.character() around your code seems to fix the problem for me:
> ifelse(data$classcode=='OCAL'& data$fish_tl<20,
+ 'OCALYOY',as.character(data$classcode))
#-----
[1] "BFRE" "BFRE" "BFRE" "HARG" "OCALYOY" "OYT" "OYT" "PFUR"
[9] "SPAU" "BFRE" "OCALYOY" "OCAL"
If this isn't it, can you make your question reproducible by adding the output of dput() to your question instead of the text representation?

Resources