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)
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
The community reviewed whether to reopen this question 11 months ago and left it closed:
Duplicate This question has been answered, is not unique, and doesn’t differentiate itself from another question.
Improve this question
How can I scale(x) only certain columns of a dataframe? I have a dataframe with 7 columns and I want to scale only column 3 and 6. The rest should stay as it is.
We can do this with lapply. Subset the columns of interest, loop through them with lapply, assign the output back to the subset of data. Here, we are using c because the outpuf of scale is a matrix with a single column. Using c or as.vector, it gets converted to vector
df[c(3,6)] <- lapply(df[c(3, 6), function(x) c(scale(x)))
Or another option is mutate_at from dplyr
library(dplyr)
df %>%
mutate_at(c(3,6), funs(c(scale(.))))
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 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.
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"])