Displaying 20 results from an estimated 50000 matches similar to: "How to drop variables using a wildcard and logic..."
2006 Feb 21
6
How to sum values across multiple variables using a wildcard?
I have a dataframe called "data" with 5 records (in rows) each of
which has been scored on each of many variables (in columns).
Five of the variables are named var1, var2, var3, var4, var5 using
headers. The other variables are named using other conventions.
I can create a new variable called var6 with the value 15 for each
record with this code:
> var6=var1+var2+var3+var4+var5
2007 Apr 16
2
Histograms of lots of variables
Hi R-helpers,
I wish to produce frequency histograms of all of the variables in my
dataframe (except some identifying variables).
I have tried
>hist(dataframe[,3:20])
to produce histograms of the 3rd through 20th variables in my dataframe, but
R doesn't like that.
Could anyone provide a suggestion?
Also, once I produce the histograms, I'd like to save them as graphic files
on my
2006 Aug 07
2
Retain only those records from a dataframe that exist in another dataframe
Dear R community,
I have two dataframes "first" and "second" which share a unique identifier.
I wish to make a new dataframe "third" retaining only the rows in
"first" which also occur in "second".
I have tried using merge but can't seem to figure it out. Any ideas?
Thanks!
Mark
2006 Feb 24
3
Sorting alphanumerically
I'm trying to sort a DATAFRAME by a column "ID" that contains
alphanumeric data. Specifically,"ID" contains integers all preceeded
by the character "g" as in:
g1, g6, g3, g19, g100, g2, g39
I am using the following code:
DATAFRAME=DATAFRAME[order(DATAFRAME1$ID),]
and was hoping it would sort the dataframe by ID in the following manner
g1, g2, g3, g6, g19,
2006 Feb 24
2
Sorting a dataframe by one column?
Given the following dataframe:
A=1:10
B=(a-5)^2
DATAFRAME=data.frame(A,B)
How can I sort DATAFRAME increasing (or decreasing, for that matter)
by B without making reference to A, or any other column?
I've read ?order and ?sort but cannot seem to figure this out.
Thank you.
Mark
2008 Mar 25
2
Combining logical operators to extract columns from a dataframe
Hi R-helpers,
I have a dataframe (called data) with 100 columns, the columns of which are
named with integers ranging from 1900 to 1999.
I wish to extract those columns which names are >=1950 and <=1970.
I tried:
data2<-subset(data,select=(names(data)>=1950 & names(data)<=1970))
but that doesn't work.
Any ideas?
Thanks! Mark
[[alternative HTML version deleted]]
2009 Aug 13
2
How to rename columns that start with numbers?
Hello,
My dataframe has new columns that start with the number 1 or 2 (resulting
from a reshape cast command).
Instead of having these columns automatically renamed by R so start with the
letter X, I would like to rename these columns to start with the characters
"SURV_" (e.g., SURV_1, SURV_2).
I can't seen to use grep() to identify and rename the columns starting with
either 1 or
2009 Jun 02
3
How to convert blanks to NA
Hi R-helpers,
I have imported data from Excel using the following code:
library(xlsReadWrite)
data <- read.xls(data,colClasses=c("character"))
and this results in all of the empty (blank) cells in the imported
Excel file also being empty (blank) in the resulting dataframe.
I am not used to having blanks (rather NAs) and I think these are
caused by the colClasses argument.
I would
2009 Jun 23
4
Apply as.factor (or as.numeric etc) to multiple columns
Hi R-helpers,
I have a dataframe with 60columns and I would like to convert several
columns to factor, others to numeric, and yet others to dates. Rather
than having 60 lines like this:
data$Var1<-as.factor(data$Var1)
I wonder if it's possible to write one line of code (per data type,
e.g. factor) that would apply a function (e.g., as.factor) to several
(non-contiguous) columns. So, I
2009 Dec 07
2
How to apply five lines of code to ten dataframes?
Hello R-helpers,
I have 10 dataframes (named data1, data2, ... data10) and I would like to
add 5 new columns to each dataframe using the following code:
data1$LogDepth<-log10(data1[,2]/data1[,4])
data1$LogArea<-log10(data1[,3]/data1[,5])
data1$p<-2*data1[,6]/data1[,7]
data1$Exp<-data1[,2]^(2/data1[,8])
data1$s<-data1[,3]/data1[,9]
...but I would prefer not to repeat this chunk of
2009 Jun 17
2
How to translate a dataframe into the R code that makes that dataframe?
Hi,
I am helping another R user (off list) and I would like to email her
an R script containing the data she needs and the code to solve her
problem. I have made a small dummy dataset, but instead of sending her
a CSV I would prefer to send the data embedded in the script, so there
would be a like in the script like:
my.df<-c( etc, etc, etc
I have made the dataframe (in a spreadsheet) and
2011 Aug 15
1
gsub wildcard
Hello all,
I have what I think is a simple question but I've been unable to solve it. I
have the following string:
A[&states=1]:[&rate=2]425, B[&states=3]:[&rate=5]500
I would like to combine the two expressions in the [], so that only one set
of [] is present after each letter, so that I have the following string:
A:[&states=1,&rate=2]425,
2009 Jun 16
3
How to extract all rows that contain the value of X in any column?
Hi R-helpers,
I'm trying to use this code
> pvh_dnv<-pvh[sapply(pvh=="dnv"),]
to make a new dataframe containing the rows from pvh that contain the
value of "dnv" in ANY column.
But, it's not working. I get this error
Error in match.fun(FUN) : element 1 is empty;
the part of the args list of 'is.function' being evaluated was:
(FUN)
which, to
2009 Jul 15
1
Averaging dataframes that are stored in a list
Dear R-helpers,
I have a list containing 5000 elements, each element is a dataframe
containing one ID column (identical over the 5000 dataframes) and 9 numeric
variables, e.g.
ID VAR1 VAR2 VAR3 ... VAR9
I would like to create a new dataframe containing the ID column and the mean
values of the 9 numeric variables. So, the structure of this new dataframe
would be identical to the structure of the
2013 Jan 17
2
Naming an object after another object...can it be done?
Hello R-helpers,
I have run the following line of code:
x<-dat$col
and now I would like to assign names(x) to be "dat$col" (e.g., a character
string equal to the column name that I assigned to x).
What I am trying to do is to assign columns in my dataframe to new objects
called x and y. Then I will use x and y within a new function to make plots
with informative axis labels (e.g.,
2006 Feb 25
2
Adding header lines to a dataframe that is exported using write.csv
I would like to export a dataframe to a .csv using:
>write.csv(dataframe,"dataframe.csv")
but I need to add four "header" lines to the csv that are not part of
the dataframe (which itself has a line of column headers).
The difficulty (for me, at least!) lies in the requirement that
certain elements of the header (X, Y and the number of "Q"s - please
see example
2009 May 04
1
Please help me subset this dataframe, thanks...
Dear R-helpers,
I have a dataframe called "trackpoints" with several columns including
a column called "time", eg:
> trackpoints
?? ? ? ?time
1 ? 12:00:00
2 ? 12:00:01
3 ? 12:00:02
.
.
.
298 12:04:57
299 12:04:58
300 12:04:59
I also have a dataframe called "data" that contains columns called
"ID", "start" and "stop", eg:
> data
2009 Jun 29
2
How to select partially (not completely) unique rows?
Dear R-helpers,
I know how to use unique to select unique rows, e.g.
unique.rows<-unique(dataframe)
but I would like to select those rows that are unique only only TWO of my
dataframe's columns (so, two rows with the same value on these two columns
would not be kept, even if they had different values in other columns).
For example, I have a dataframe with 10 columns, two of which are
2009 Jul 09
2
How to combine two rows (in a dataframe) into a third row?
Dear R-helpers,
I have two rows in my dataframe:
ID VALUE
1A 10
1B 15
and I would like to combine these two rows into a single (new) row in my
dataframe:
ID VALUE
1 25
...simply by specifying a new value for ID and summing the two VALUES.
I have been trying to do this with with rbind, but it's not working.
I'd appreciate any pointers.
Thanks, Mark Na
[[alternative
2011 Oct 08
1
How to write a list object's name to a new dataframe in that list object
Hello R-helpers,
I have a list that only contains dataframes. Each element of the list
(i.e., each dataframe) has a unique name ("one" through "ten"). I wish
to add a new column (called "NAME") to each list element (i.e each
datarame) and I want that column to contain the name of it's list
element.
e.g. the list element (i.e., dataframe) called "one"