简体   繁体   English

从最后一行复制值并粘贴到第一行r

[英]copy value from last row and paste in to first row r

I have a dataframe. 我有一个数据框。 In it the values in col3 and col4 are in the wrong row by 1. The bottom row values are supposed to be in the top row, the top row should be in the second and so on 在其中,col3和col4中的值在错误的行中乘以1。应该将最下面一行的值放在第一行,最上面一行的应该在第二行,依此类推。

Currently 目前

col1 col2  col3  col4
 a    b      c     d
 e     f     g     h
 i      j    k     l

should be 应该

 col1 col2  col3  col4
  a    b      k     l
  e     f     c     d
   i    j     g     h

how do I move just the values in col3 and col4 by one down with the last becoming the first? 如何仅将col3和col4中的值向下移动,最后一个变为第一个?

I tend to use dplyr's mutate_each and summarise_each functions to apply the same function(s) to multiple columns. 我倾向于使用dplyr的mutate_eachsummarise_each功能相同的功能(S)适用于多列。 Here's how you could approach it with a custom "swap" function for better readability: 这是通过自定义“交换”功能实现更好的可读性的方法:

library(dplyr)

Define a function: 定义一个函数:

swap <- function(x) c(last(x), head(x, -1L))

Now you can use this custom function inside "mutate_each" and specify the columns you want to apply the function to: 现在,您可以在“ mutate_each”中使用此自定义函数,并指定要将函数应用到的列:

mutate_each(df, funs(swap), col3, col4)
#  col1 col2 col3 col4
#1    a    b    k    l
#2    e    f    c    d
#3    i    j    g    h

If you prefer base R, you could do it similarly, using a slightly modified function "swap2" and "lapply" to apply the function to multiple columns: 如果您更喜欢基数R,则可以使用稍作修改的函数“ swap2”和“ lapply”将其应用于多个列,以类似的方式进行操作:

# define the function:
swap2 <- function(x) c(tail(x, 1L), head(x, -1L))
# define the columns you want to apply the function to:
cols <- c("col3", "col4")
# Finally, lapply over the data:
df[cols] <- lapply(df[cols], swap2)

data: 数据:

> dput(df)
structure(list(col1 = c("a", "e", "i"), col2 = c("b", "f", "j"
), col3 = c("c", "g", "k"), col4 = c("d", "h", "l")), .Names = c("col1", 
"col2", "col3", "col4"), class = "data.frame", row.names = c(NA, 
-3L))

Assume d is your data.frame: 假设d是您的data.frame:

d$col3 <- c(d$col3[length(d$col3)], d$col3[-length(d$col3)])
d$col4 <- c(d$col4[length(d$col4)], d$col4[-length(d$col4)])

Try this 尝试这个

df <- data.frame(col1=c("a", "e", "i"),
                 col2=c("b", "f", "j"),
                 col3=c("c", "g", "k"),
                 col4=c("d", "h", "l"))


df <- cbind(df[, 1:2], df[c(dim(df)[1], 1:(dim(df)[1]-1)), 3:4])

Create the data frame using characters rather than factors: 使用字符而不是因素创建数据框:

df <- data.frame(col1=c("a", "e", "i"),
                 col2=c("b", "f", "j"),
                 col3=c("c", "g", "k"),
                 col4=c("d", "h", "l"), stringsAsFactors=FALSE)

df$col3 <- c(df$col3[nrow(df)], df$col3[1:(nrow(df)-1)])
df$col4 <- c(df$col4[nrow(df)], df$col4[1:(nrow(df)-1)])

Output: 输出:

> df
  col1 col2 col3 col4
1    a    b    k    l
2    e    f    c    d
3    i    j    g    h

Assuming that df is your dataframe, you can use a for loop 假设df是您的数据帧,则可以使用for循环

temp3 = df[nrow(df),3]
temp4 = df[nrow(df),4]
for(i in 2:nrow(df)){
    df[(i,3] = df[((i - 1),3]
    df[(i,4] = df[((i - 1),4]
}
df[1, 3] = temp3
df[1, 4] = temp4

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM