Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I'd like to store a substring value from the "Date" column into another variable.
This is what my data frame looks like:
My data frame is called "test".
What I want to achieve is to take the last two numbers of the Date values and store them into another data frame. So the end result would look like this:
Value
1 01
2 01
3 01
This is what I've done already:
t.sub<-substring(test$Data, 5,6)
However, R returned this:
character(0)
and:
t.sub<-data.frame(substring(test$Data,5,6))
R returned this:
[1] substr(molten$Data, 5, 6)
<0 rows> (or 0-length row.names)
You can try gsub, first make sure to convert your date to character:
gsub('.*([0-9]{2})$', '\\1',as.character(test$Data))
For string related questions I would highly recommend using the package stringr. And here's my solution.
require(stringr)
Date <- as.character(201101)
Date <- str_sub(Date, start=5L, end=6L)
You could replace the Date using your data frame such as df$Date.
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 months ago.
Improve this question
library(quantmod)
quote<-c("0883.HK","0386.HK","0857.HK")
df1<-data.frame(getQuote(quote)$Last)
Just download the last traded price for some stocks and put those quotes in a data frame, and then what I am trying to do here is to add column names for those quotes like below:
colnames(df1)<-c("883","386","857")
Error in names(x) <- value :
'names' attribute [3] must be the same length as the vector [1]
May I ask what have I done wrong here? Million thanks.
The data you selected is one column with three observations. Therefore you cannot assign it three names as there is only one column
> df1
getQuote.quote..Last
1 10.00
2 3.74
3 3.55
When we apply as.data.frame or data.frame on a vector (after extracting the Last column), it will be a single column with that vector as values of the column. We may need to convert to list and then setting the column names will work
df1 <- as.data.frame.list(getQuote(quote)$Last)
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I have a text file myfile.txt that looks like this:
10
20
30
40
I try to load it in R using:
nums <- read.csv('myfile.txt', header=FALSE);
However, this returns a list. What I want is the equivalent of:
nums <- c(10, 20, 30, 40);
I later have code that does the following:
v = sprintf("%d", nums);
This works fine when I have the vector version, but when I try to load my data from file, I get the following error:
Error in sprintf("%d", nums) : unsupported type
Execution halted
Does nobody use scan anymore??
> scan("./myfile.txt")
Read 4 items
[1] 10 20 30 4
I mistyped the fourth item in the file.
Returns exactly what you wanted:
> n = scan("./myfile.txt")
Read 4 items
> identical(n, c(10,20,30,4))
[1] TRUE
When you use read.csv, the output should be a data.frame.
If you want to access the column of integers, try nums[[1]]. (With nums being the output read.csv.)
To convert to a vector, use unlist(nums).
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I have a huge dataframe that contains a column of gene's IDs. Each Gene ID appears in the column in a different number of times.
I want to extract from the dataframe a column that presents every Gene ID once, and at the same time I want to keep the data as a dataframe and not to change it to a list with factors.
example:
GeneID
589034
489034
589034
589034
48999
99449
99449
And i want my output to be:
GeneID
589034
489034
48999
99449
You can use the unique function for this:
dat = c('GeneID', '589034', '489034', '589034', '589034', '48999', '99449', '99449')
unique(dat)
[1] "GeneID" "589034" "489034" "48999" "99449"
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I want to add a first column with consecutive numbers with characters in a existing data frame.
I use the following code. It does not work.
df$VARNAME_ <- paste0('COL', 1:5)(df)
I want to it look like this.
VARNAME_ old_var1 old_var2
COL1 1 2
COL2 1 2
COL3 1 2
COL4 1 2
COL5 1 2
Thanks in advance.
I am Sorry that I asked a stupid question. And now I figure out.
The solution is as following.
actual_df<-data.frame(df)#transfer matrix a to data frame
actual_df<-cbind(VARNAME_=paste0('COL', 1:5),actual_df) #add COL1~COL5 in the first column
actual_df<-cbind(ROWTYPE_ = 'PROX', actual_df) #Add a variable with constant observations in first column. Now the previous column become second one.
df$VARNAME_ = paste0('COL', 1:5)
will work
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
Trying to merge two data frames, using a variable called hash_id. For some reason R does not recognize the hash-id's in one of the data frames, while it does so in the other.
I have checked and I just don't get it. See below how I checked:
> head(df1[46],1) # so I take the first 'hash-id' from df1
# hash_id
# 1 abab123123
> which(df2 == "abab123123", arr.ind=TRUE) # here it shows that row 6847 contains a match
# row col
# [1,] 6847 32`
> which(df1 == "abab123123", arr.ind=TRUE) # and here there is NO matching value!
# row col
#
One possibility is trailing or leading spaces in the concerned columns for one of the datasets. You could do:
library(stringr)
df1[, "hash_id"] <- str_trim(df1[,"hash_id"])
df2[, "hash_id"] <- str_trim(df2[, "hash_id"])
which(df1[, "hash_id"]=="abab123123", arr.ind=TRUE)
which(df2[, "hash_id"]=="abab123123", arr.ind=TRUE)
Another way would be use grep
grepl("\\babab123123\\b", df1[,"hash_id"])
grepl("\\babab123123\\b", df2[,"hash_id"])