I'm trying to export a list of 0's and 1's in R using the following code:
write(export, file="export.txt", ncol=1)
However, in the file "export.txt," there are 1's and 2's instead of 0's and 1's. How do I get the exported file to have 0's and 1's?
R List: 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 1 0 1 1
This is what shows up in the file: 1 1 1 1 1 1 1 2 1 1 1 1 1 1 2 1 2 1 2 2
I suspect that export is a factor variable. write is a wrapper for cat and cat doesn't seem to gracefully handle factors:
x <- factor(0:1)
cat(x)
## 1 2
You can coerce to character to get the proper output:
cat(as.character(x), file="export.txt")
## 0 1
Related
I have two vectors in R
Vector 1
0 0 0 0 0 0 0 0 0 0
Vector 2
1 1 3 1 1 1 1 1
I need to put the values from vector 2 into vector 1 but into specific positions so that vector 1 becomes
1 1 3 0 0 1 1 1 1 1
I need to do this in one line of code. I tried doing:
vector1[1:3,6:10] = vector2[1:3,4:8]
but I am getting the error "incorrect number of dimensions".
Is it possible to do this?
vector1[c(1:3,6:10)] = vector2[c(1:3,4:8)]
> vector1
[1] 1 1 3 0 0 1 1 1 1 1
We may use negative indexing
vector1[-(4:5)] <- vector2
vector1
[1] 1 1 3 0 0 1 1 1 1 1
I used the code below for a total of 25 variables and it worked.It shows up as either 1 or 0:
jb$finances <- ifelse(grepl("Finances", jb$content.roll),1,0)
I want to be able to add the number of "1" s in each row across the multiple of selected column/variables I just made (using the code above) into another column called "sum.content". I used the code below:
jb <- jb %>%
mutate(sum.content=sum(jb$Finances,jb$Exercise,jb$Volunteer,jb$Relationships,jb$Laugh,jb$Gratitude,jb$Regrets,jb$Meditate,jb$Clutter))
I didn't get an error using the code above, but I did not get the outcome I wanted.
The result of this was 14 for all my row.I was expecting something <9 since I only selected for 9 variables.I don't want to delete the other variables like V1 and V2, I just want to focus on summing some variables.
This is what I got using the code:
V1 V2... Finances Exercise Volunteer Relationships Laugh sum.content
1 1 1 1 1 0 14
2 0 1 0 0 1 14
2 0 0 0 0 1 14
This is What I want:
V1 V2... Finances Exercise Volunteer Relationships Laugh sum.content
1 1 1 1 1 0 4
2 0 1 0 0 1 1
2 0 0 0 0 1 1
I want R to add the number of 1's in each row(within the columns I want to select). How would I go about incorporating the adding of the 1's in code(from a set of variable/column)?
Here is an answer that uses dplyr to sum across rows of variables starting with the letter V. We'll simulate some data, convert to binary, and then sum the rows.
data <- matrix(rnorm(100,100,30),nrow = 10)
# recode to binary
data <- apply(data,2,function(x){x <- ifelse(x > 100,1,0)})
# change some of the column names to illustrate impact of
# select() within mutate()
colnames(data) <- c(paste0("V",1:5),paste0("X",1:5))
as.data.frame(data) %>%
mutate(total = select(.,starts_with("V")) %>% rowSums)
...and the output, where the sums should equal the sum of V1 - V5 but not
X1 - X5:
V1 V2 V3 V4 V5 X1 X2 X3 X4 X5 total
1 1 0 0 0 1 0 0 0 1 0 2
2 1 0 0 1 0 0 0 1 1 0 2
3 1 1 1 0 1 0 0 0 1 0 4
4 0 0 1 1 0 1 0 0 1 0 2
5 0 0 1 0 1 0 1 1 1 0 2
6 0 1 1 0 1 0 0 1 1 1 3
7 1 0 1 1 0 0 0 0 0 1 3
8 1 0 0 1 1 1 0 1 1 1 3
9 1 1 0 0 1 0 1 1 0 0 3
10 0 1 1 0 1 1 0 0 1 0 3
>
Given a vector:
x = c(1,0,1,0)
can be arranged in the following manner:
> m
row1: 1 1 0 0
row2: 1 0 1 0 # <- identical
row3: 0 1 1 0
row4: 1 0 0 1
row5: 0 1 0 1
row6: 0 0 1 1
I would like to calculate how many diviations, or changes are required to each vector in each row one would have to perform to end up with the original vector x
result <- function(m,x)
> result
Var1 Var2 Var3 Var4 changes_from_x
1 1 0 0 1
1 0 1 0 0
0 1 1 0 1
1 0 0 1 1
0 1 0 1 2
0 0 1 1 1
this is different from just comparing if the vector is the same Compare two vector in R
Check whether two vectors contain the same (unordered) elements in R
or to simply state that it's the wrong order or not Test Match and Order between two vectors in R as the method would compare how many diviations has occured between each vector in the matrix and the original vector.
I have a dataset which looks this this:
A B X50_TT_1.0 X50_TT_1.1 X60_DD_2.0 X60_DD_2.1 X100_L2V_7.0 X100_L2V_7.1
3 1 1 0 0 1 1 0
6 3 0 1 0 1 0 1
2 3 1 0 0 1 1 0
10 5 0 1 1 0 1 0
0 0 1 0 1 0 0 1
I want to have new data frame (df) which only contains columns which ends with 1.1, 2.1 i.e.
df
X50_TT_1.1 X60_DD_2.1 X100_L2V_7.1
0 1 0
1 1 1
0 1 0
1 0 0
0 0 1
As here I only shows few columns but actually it contains more than 100 columns. Therefore, kindly provide the solution which can be applicable to as many columns dataset consists.
Thanks in advance.
I guess the pattern is, that the column ends on ".1" may you need to adapt it at that point.
My data I am using
original_data
A B X50_TT_1.0 X50_TT_1.1 X60_DD_2.0 X60_DD_2.1 X100_L2V_7.0 X100_L2V_7.1
1 3 1 1 0 0 1 1 0
Actually this is for everything ending with "1"
df <- original_data[which(grepl(".1$", names(original_data)))]
For ending with ".1" you have to use:
df <- original_data[which(grepl("\\.1$", names(original_data)))]
For original_data both gave me the same result:
df
X50_TT_1.1 X60_DD_2.1 X100_L2V_7.1
1 0 1 0
I am working on educational assignment to produce an Incidence matrix from a BIB design using R language software.
I found a web page http://wiki.math.yorku.ca/index.php/R:_Incidence_matrix related to problem. But it produces Data matrix instead of Incidence matrix. can anyone please help me out with R language code. the codes for obtaining the BIB design matrix is:
b=4 # Number of Blocks
t=8 # Number of Column
z=c(1,2,3) # Shift
m=NULL
y=c(0)
w=c(y,cumsum(z) %%t) # cumsum() is for the running totals
p=seq(from=0, to=t-1, by=1)
l=NULL
for(i in 1:b)
{
for(j in 1:t)
{
l=c(l,rep((w[i]+p[j]+t)%% t))
}
}
#"BIB design" it has 4 rows (blocks b) and 8 column (treatments t)
x= matrix(c(l),nrow=b,ncol=t,byrow = TRUE)
print (x)
0 1 2 3 4 5 6 7
1 2 3 4 5 6 7 0
3 4 5 6 7 0 1 2
6 7 0 1 2 3 4 5
(it can be generated at any t-treatments and b-blocks size generally)
using above design matrix x (4*8). i need the following Incidence matrix (8*8)
1 1 0 1 0 0 1 0
0 1 1 0 1 0 0 1
1 0 1 1 0 1 0 0
0 1 0 1 1 0 1 0
0 0 1 0 1 1 0 1
0 1 0 0 1 0 1 1
1 0 1 0 0 1 0 1
Consider Design Matrix Column wise and generate Incidence Matrix Row wise. For example the 1st column of x is
0
1
6
3
Now see the 1st row of the required Incidence Matrix (IM).
1 1 0 1 0 0 1 0
At 1st place of x is 0 so put 1 in 1st place of IM.
At 2nd place of x is 1 so put also 1 at the 2nd place of IM.
Here 2 is missing in the column of x so put 0 at 3rd place of IM.
x contains 3 so put 1 at 4th place, 4 and 5 is missing put two 0's in a row consecutively.
X has 6 put 1 at 7th place and 7 is missing put 0 at 8th place of IM.
Take 2nd column of x and similarly filled 2nd row of IM. If the particular number (0 to 7) is present put one otherwise zero.
I hope, i make it clear for every one now.
Making the x matrix different to have two identical entries in one column I get this logic to work:
x[4,1] <- 1
t( apply(x, 2, function(z){ ret <- numeric(8)
for( i in seq_along(z) ){ret[z[i]+1] <- ret[z[i]+1]+ 1}
ret}) )
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,] 1 2 0 1 0 0 0 0
[2,] 0 1 1 0 1 0 0 1
[3,] 1 0 1 1 0 1 0 0
[4,] 0 1 0 1 1 0 1 0
[5,] 0 0 1 0 1 1 0 1
[6,] 1 0 0 1 0 1 1 0
[7,] 0 1 0 0 1 0 1 1
[8,] 1 0 1 0 0 1 0 1
I'm not exactly sure how you are going by getting your intended output. However, the reason you are getting a much longer output than you anticipated is possibly due to the [ as.factor(vec),] part of your code .
as.factor(vec) is taking your 4x4 matrix and turning it into a single vector of 16 elements. (Well, technically, vec is already a vector, but let's not confuse things).
as.factor(vec)
[1] 0 1 3 2 1 2 0 3 2 3 1 0 3 0 2 1
Levels: 0 1 2 3
You are then using that as an index, which is repeating values of A.
** By the way, are you sure you should get a matrix of all 1's? And not perhaps just 1's on the diagonal?
contrasts( as.factor(vec), contrasts =FALSE)
# 0 1 2 3
# 0 1 0 0 0
# 1 0 1 0 0
# 2 0 0 1 0
# 3 0 0 0 1