Print horizontally with write.table command - r

Here i am writing a small code where number needs to be printed horizantally in the txt file which is been generated here as "note.txt"
for(n in 1:4)
{
write.table(n,"note.txt",append = TRUE)
}
I am getting output like
"x"
"1" 1
"x"
"1" 2
"x"
"1" 3
"x"
"1" 4
Whereas i want output as :
1 2 3 4
or
1,2,3,4
Please help me.

Paste function in R can be used to combine a vector/strings using a separator ( Use the attribute collapse in paste command to specify the separator).
If the vector v contains the set of numbers to be printed horizontally.
v=c(1:4);
write(paste(as.character(v), collapse=","),"note.txt",append="TRUE";

Related

Finding consecutive values in a string in R

I am trying to find 3 or more consecutive "a" within the last 10 letters of my data frame string. My data frame looks like this:
V1
aaashkjnlkdjfoin
jbfkjdnsnkjaaaas
djshbdkjaaabdfkj
jbdfkjaaajbfjna
ndjksnsjksdnakns
aaaandfjhsnsjna
I have written this code, however it just gets out the number of consecutive "a" within the whole string. However, I am wanting to do it so it only looks at the last 10 digits and then prints the string where the consecutive "a" are found. The code I have wrote is:
out: [1] 3
I am wanting my output to look like this:
jbfkjdnsnkjaaaas
djshbdkjaaabdfkj
jbdfkjaaajbfjna
Can anyone help
Using regex, you could do:
grep("(?=.{10}$).*?a{3,}", string, perl = TRUE, value = TRUE)
[1] "jbfkjdnsnkjaaaas" "djshbdkjaaabdfkj" "jbdfkjaaajbfjna"
string <- c("aaashkjnlkdjfoin", "jbfkjdnsnkjaaaas", "djshbdkjaaabdfkj",
"jbdfkjaaajbfjna", "ndjksnsjksdnakns", "aaaandfjhsnsjna")
If you have a dataframe and need tosubset it:
subset(df, grepl("(?=.{10}$).*?a{3}",V1, perl = TRUE))
V1
2 jbfkjdnsnkjaaaas
3 djshbdkjaaabdfkj
4 jbdfkjaaajbfjna

How to remove constant parts of a string in R

I would like to remove constant (shared) parts of a string automatically and retain the variable parts.
e.g. i have a column with the following:
D20181116_Basel-Take1_digital
D20181116_Basel-Take2_digital
D20181116_Basel-Take3_digital
D20181116_Basel-Take4_digital
D20181116_Basel-Take5_digital
D20181116_Basel-Take5a_digital
how can i get automatically to for any similar column (here removing: "D20181116_Basel-Take" and "_digital"). But the code should be find the constant part itself and remove them.
1
2
3
4
5
5a
I hope this is clear. Thank you very much.
You can do it with a regex: it will remove everything before 'Take' and after the underscore character:
vec<- c("D20181116_Basel-Take1_digital",
"D20181116_Basel-Take2_digital",
"D20181116_Basel-Take3_digital",
"D20181116_Basel-Take4_digital",
"D20181116_Basel-Take5_digital",
"D20181116_Basel-Take5a_digital")
sub(".*?Take(.*?)_.*", "\\1", vec)
[1] "1" "2" "3" "4" "5" "5a"
with gsub():
assuming you have a dataframe df and want to change column
df$column <- gsub("^D20181116_Basel-Take","",df$column)
df$column <- gsub("_digital$","",df$column)

Add letter at the beginning of string

I am trying to change the name of some columns in my data using str_c function ( after installing "stringr" package). Column names are as follow:
> x
a b c d
I need to change the the "c" and "d" with "Aa" and "Ab". So instead of writing the full column names in my command, I will use the following structure:
colnames(x[,3:4])<-str_c(colnames(x[,1:2], "A")
However, the result shows the "A" at the end not the beginning of the name. So how to put the "A" at the beginning to get the following:
> x
a b Aa Ab
If I understand your question and this is for displaying a "column name", then you should swap the order in your expression, this
colnames(x[,3:4])<-str_c(colnames(x[,1:2], "A")
should be
colnames(x[,3:4])<-str_c("A", colnames(x[,1:2])
Note column names with a leading digit won't work with the $ function unless the name is quoted. So "A" works fine with '$' function but "1" needs the name to be quoted.

converting a text file into a vector in R

I have a text file in the following format: elt1\telt2\t... with 1,000,000 elements.
Most of these elements are integers, but some of them are of the form number_number or chainOfCharacters. For example: 1\t2\t2_3\t4_44\t2\t'sap'\t34\t'stack' should output: 1 2 2_3 4_44 2 'sap' 34 'stack'.I tried to load this data in R using data <- read.table(file(fileName),row.names=0,sep='\t') but it is taking for ever. Is it possible to speed this up?
You should use scan instead:
scan(fileName, character(), quote = "")
# Read 8 items
# [1] "1" "2" "2_3" "4_44" "2" "'sap'" "34" "'stack'"

Extracting from text file in R

I need to compare two .txt files with the following formats , with R:
rows in file1:
1-11!AIVDM,1,1,,B,11b4N?#P?w<tSF0l4Q#>4?wp1`Oo,0*3D
1347204643
2-12$GPRMC,153102,A,6300.774,N,05238.627,W,12.9,186,090912,30,W*79
1347204664
( here for some reason the time (1347204643) is in the separate row)
rows in file2:
#1:1347204643:11!AIVDM,1,1,,B,11b4N?#P?w<tSF0l4Q#>4?wp1`Oo,0*3D
#2:1347204664:12$GPRMC,153102,A,6300.774,N,05238.627,W,12.9,186,090912,30,W*79
I am interested only in verifying if the same ID, which is in the beginning of the row (e.g. 1 and 2 here), exists in both files ( if the ID that exists in file1 exists also in file2).
Can someone help me with this? Thank you very much in advance!
You can do something like this :
First you read 2 two files using readLines
ll1 <- readLines(textConnection('#1:1347204643:11!AIVDM,1,1,,B,11b4N?#P?w<tSF0l4Q#>4?wp1`Oo,0*3D
#2:1347204664:12$GPRMC,153102,A,6300.774,N,05238.627,W,12.9,186,090912,30,W*79'))
ll2 <- readLines(textConnection('1-11!AIVDM,1,1,,B,11b4N?#P?w<tSF0l4Q#>4?wp1`Oo,0*3D
1347204643
2-12$GPRMC,153102,A,6300.774,N,05238.627,W,12.9,186,090912,30,W*79
1347204664'))
Do some treatments
#Remove '#` fom the first files
ll1 <- gsub('#','',ll1)
#Take only the odd lines from the second file
ll2 <- ll2[c(TRUE,FALSE)]
Extract the index of each lines using substr
ll1 <- substr(ll1,1,1)
ll2 <- substr(ll2,1,1)
Now you have this 2 lists :
ll1
[1] "1" "2"
> ll2
[1] "1" "2
To compare you can use match
match(ll1,ll2)
[1] 1 2

Resources