I am adding together columns with the following code:
cbind(rowSums(data[,c(5,10,11,15)],na.rm=TRUE),rowSums(data[,c(3,6,7)],na.rm=TRUE))
But I want to add the columns based upon the column name not their number to eliminate confusion....
Is there an easy way to do this?
Yep, just replace the numbers with the column names in quotes:
cbind(rowSums(data[,c('elephant','giraffe')],na.rm=TRUE),
rowSums(data[,c('fish','penguin','albatross')],na.rm=TRUE))
I'm collecting data on animal species today. Not sure why I'm adding them together.
Related
I need to replace values in the Nth column of my df, call these values v1s, by some other values from anothe df, call them v2s. There is a dictionary, or ruther two dictionaries. The first one translates v1s into numbers, the second one translates the numbers into v2s. I tried merge(), left/right_join(), smth else...but nothing seems to work. Can somebody help please?
Merging the datasets should work. Try the code until you can make it work.
Otherwise, you can always simply add an extra column to your dataset with
datasetA$newvar <- datasetB$v2s
when you have correctly added the second variable, simply drop the first.
I'm trying to write a script in which within my df, I want to find in several consecutive columns (e.g. 16:25) a specific string which is a combination of "SA (string and spaces within the bracket)". If the string is present in any of the columns, it will only appear in one of them.
When that string is detected, I would like it to be moved or copied to a different column named "SA_status".
If it is not found in any of the columns, then I'd like the value for that row on SA_status to appear as NA.
I've tried to create a for loop to search in the first column, and if it wasn't found, in the next one and so on; and if it was found to stop there. Then, I have used a combination of the functions mutate(), case_when(), str_detect() to copy that value into the new column.
Finally, if after doing this process the values in the new column were still blank, I've assigned them to NA.
However, I must be doing something wrong because it is not working and I'm getting really desperate.
Could you please give me a hand? Cheers!
I am going to do repeated measures ANOVA on my data, but to this point, my data is wide. Two independent (categorical) variables are spread across single responsive variable.
See the image: https://imgur.com/1eTWSIM
I want to create two categorical variables that take values from the different parts of the columns (circled on the screenshot). Subject numbers should be kept as a category. So after using gather() function, the data should look something like this:
https://imgur.com/SGM2N69
I've seen in a tutorial (that I can't find anymore) that you can create two columns from a single function, using different parts of the colnames (using "_" as a separator), but I can't exactly remember how it was done.
Any help would be appreciated and ask if anythings is not clear in my explanation.
I have solved the problem by using 'gather()' function first and then 'separate()' to separate it into two new columns. So I guess, if you want to make two key columns, first you have to make a single column containing both values and later separate it into two.
At least that is how I did it.
I want to update my dataframe adding value in various columns.
My dataframe has A-Z columns name.
I created a list specifiying which column want to update.
upd<-c("a","b","c","d")
And i used simple R syntax to update
df<-df[,names(df)%in%upd]+5
But I only have a,b,c,d columns.
is there one way to save all columns?.
thanks
This is probably a basic question, but why does R think my vector, which has a bunch of words in it, are numbers when I try to use these vectors as column names?
I imported a data set and it turns out the first row of data are the column headers that I want. The column headers that came with the data set are wrong ones. So I want to replace the column names. I figured this should be easy.
So what I did was I extracted the first row of data into a new object:
names <- data[1,]
Then I deleted the first row of data:
data <- data[-1,]
Then I tried to rename the column headers with the "names" object:
colnames(data) <- names
However, when I do this, instead of changing my column names to the words within the names object, it turns it into a bunch of numbers. I have no idea where these numbers come from.
Thanks
You need to actually show us the data, and the read.csv()/read.table() command you used to import.
If R thinks your numeric column is string, it sounds like that's because it wrongly includes the column name, i.e. you omitted header=TRUE in your read.csv()/read.table() import.
But show us your actual data and commands used.