I'm trying to add header to the first column called "TYPE"-
this is the result I get:
this is the result I want:
thanks
I am not sure, my answer will be useful or not. But I faced same problem and I solved this problem in following way.
library(tidyverse)
rownames_to_column(mtcars,var = "Type") -> df
df
Thanks!!...
I understand correctly you want to name... the row names there is no way to do this unless you get the row names and make a column of it
You seem to think row names is a column
col.names(yourtable)[1] <- "Type"
Related
This is the code I am trying to run:
data_table<-data_table%>%
merge(new_table, by = 'Sample ID')%>%
mutate(Normalized_value = ((1.8^(data_table$Ubb - data_table$Ct_adj))*10000))
I want to first add the new column ("Ubb") from "new_table" and then add a calculated column using that new column. However, I get an error saying that Ubb column does not exist. So it's not performing merge before running mutate? When I separate the functions everything works fine:
data_table<-data_table%>%
merge(new_table, by = 'Sample ID')
data_table<-data_table%>%
mutate(Normalized_value = ((1.8^(data_table$Ubb - data_table$Ct_adj))*10000))
I would like to keep everything together just for style, but I'm also just curious, shouldn't R perform merge first and then mutate? How does order of operation during piping work?
Thank you!
you dont need to refer to column name with $ sign. i.e. use Normalized_value = ((1.8^(Ubb - Ct_adj))*10000)
because it is merged now. with $ sign I believe R, even though does the merge, has original data_table still in memory. because the assignment operator did not work yet. the assignment will take place after all operations.
Try running the code like this:
data_table<-data_table%>%
merge(new_table, by = 'Sample ID')%>%
mutate(Normalized_value = ((1.8^(Ubb - Ct_adj))*10000))
Notice that I'm not using the table name with the $ within the pipe. Your way is telling the mutate column to look at a vector. Maybe it's having some trouble understanding the length of that vector when used with the merge. Just call the variable name within the pipe. It's easiest to think of the %>% as 'and then': data_table() and then merge() and then mutate(). You might also want to think about a left_join instead of a merge.
I'm trying to use "count()" to explore the variable "Count/Territory" from the data base "fiw". Why it returns an error?
I'm triying writing it like this:
fiw %>%
count(Country/Territory)
Thank you.
The column name is unusual because of the presence of /. Can backquote it and get it working
library(dplyr)
fiw %>%
count(`Country/Territory`)
I guess the main cause of the error is the black slash /. In R to divide one column by another you can use /. For example if you need to divide the data in column A by the data in column B you can write A/B.
I have already checked the forum for the solution but could not find the solution, hence, posting the problem here.
I am trying to change a name of a column using Rename, although the command is executed without any error but the names of the columns does not change.
I use this coderename(red,parts = category) Thanks
You need to pass a named vector as the second argument, and the names need to be quoted. Also, don't forget that you need to overwrite red with the result.
red <- reshape::rename(red, c(parts = "category"))
If you want to change multiple columns, add the items to the named vector you supply to the second argument.
So this is a bit strange, I am pretty new to R and facing this weird problem.
I have a data frame, and there is a column called SNDATE which is a combined value of two different columns.
I want to check if the data frame has a column named SN, if it doesn't I will split SNDATE to fill the SN column.
Here is the code
if(!('SN' %in% colnames(data))){
#do some spliting here
}
Funny thing is, it keeps saying it's there, and the stuff in it never gets triggered.
And when I do this:
print(data$SN)
It will print the value of data$SNDATE. So does R have some sort of lazy name filling or something? This is very strange to me.
Thank you very much for the help
When you do
print(data$SN)
it works because $ is using partial name matching. For another example, try
mtcars$m
There is no column named m, so $ partially matches mpg. Unfortunately, this is not used in %in%, so you will need to use the complete exact column name in
if(!('SNDATE' %in% colnames(data))){
#do some spliting here
}
You could insead use something along the lines of pmatch()
names(mtcars)[2] <- "SNDATE"
names(mtcars)[pmatch("SN", names(mtcars))]
# [1] "SNDATE"
So the if() statement might go something like this -
nm <- colnames(data)
if(!nm[pmatch("SN", nm)] %in% nm) {
...
}
Or even
if(is.na(pmatch("SN", names(data)))
might be better
I am a beginner of R and I try to use it as much as possible to advance.
I want to add a new column to a existing csv file. This new column is the daily change of the 9th column. I wrote a code as follows:
for (i in nrow(period)) {
period$changeyr3<-period[i+1,9]-period[i,9]
}
changeyr3 is the name of the new column and I got all NAs.
Can you please help me?
Linda
You'll need to do this.
for( i in 1:nrow(period)){
period$changeyr3[i] <- period[i+1,9] - period[i,9]
}
This should work. In what you were doing, you were setting the entire column's values to each time. Also, your last value will still be NA.